message
option to indicate the error message. The error messages of built-in validators are translated into different
locales
.
| Property | Type | Description |
|---|---|---|
valid
*
|
Boolean
|
The value is valid or invalid |
message
|
String
or
Object
|
The error message |
meta
|
Object
|
Meta data that can be used when the validation completes |
en_US
and
vi_VN
, respectively:
const
strongPassword
=
function
(
)
{
return
{
validate
:
function
(
input
)
{
const
value
=
input
.
value
;
if
(
value
===
''
)
{
return
{
valid
:
true
,
}
;
}
// Check the password strength
if
(
value
.
length
<
8
)
{
return
{
valid
:
false
,
message
:
{
en_US
:
'The password must have at least 8 characters'
,
vi_VN
:
'Mật khẩu phải có ít nhất 8 ký tự'
,
}
,
}
;
}
// More checks
// ...
return
{
valid
:
true
,
}
;
}
,
}
;
}
;
FormValidation
.
validators
.
checkPassword
=
strongPassword
;
const
fv
=
FormValidation
.
formValidation
(
document
.
getElementById
(
'profileForm'
)
,
{
locale
:
'en_US'
,
localization
:
FormValidation
.
locales
.
en_US
,
fields
:
{
password
:
{
validators
:
{
checkPassword
:
{
}
,
}
,
}
,
}
,
}
)
;
// Query the locate button at the top
const
localeButtons
=
Array
.
from
(
document
.
querySelectorAll
(
'.setLocaleButton'
)
)
;
localeButtons
.
forEach
(
function
(
btn
)
{
// Handle the `click` event
btn
.
addEventListener
(
'click'
,
function
(
)
{
// Update the active state for the selected button
localeButtons
.
forEach
(
function
(
btn
)
{
btn
.
classList
.
remove
(
'active'
)
;
}
)
;
btn
.
classList
.
add
(
'active'
)
;
// Get the selected locale
const
locale
=
btn
.
getAttribute
(
'data-locale'
)
;
// Update the locale
// `fv` is the FormValidation instance
// created above
fv
.
setLocale
(
locale
,
FormValidation
.
locales
[
locale
]
)
.
resetForm
(
)
;
}
)
;
}
)
;