| Name | HTML attribute | Type | Description |
|---|---|---|---|
callback
*
|
data-fv-callback___callback
|
Function
|
The callback method |
message
|
data-fv-callback___message
|
String
|
The error message |
function
(
input
)
{
// input is an object of
// {
// value: The field value,
// options: The callback validator options
// }
// Check the field validity
return
true
;
// or false
}
valid
and
message
members:
function
(
input
)
{
// input is an object of
// {
// value: The field value,
// options: The callback validator options
// }
// ... Do your logic checking
if
(
...
)
{
return
{
valid
:
true
,
// or false
message
:
'The error message'
}
;
}
return
{
valid
:
false
,
// or true
message
:
'Other error message'
}
;
}
// You might need to change the importing path
import
callback
from
'formvalidation/dist/es6/validators/callback'
;
const
result
=
callback
(
)
.
validate
(
{
value
:
...
,
options
:
{
callback
:
...
,
}
,
}
,
}
)
;
/*
result is an object of
{
valid: true or false,
message: The error message
}
*/
// You might need to change the importing path
import
callback
from
'formvalidation/dist/es6/validators/callback'
;
// A very simple method to check the strength of a password
const
validatePassword
=
function
(
input
)
{
const
value
=
input
.
value
;
if
(
value
===
''
)
{
return
{
valid
:
true
}
;
}
if
(
value
.
length
<
8
)
{
return
{
valid
:
false
,
message
:
'Password must have at least 8 characters'
,
}
;
}
if
(
value
===
value
.
toLowerCase
(
)
)
{
return
{
valid
:
false
,
message
:
'Password must have at least one uppercase character'
,
}
;
}
if
(
value
===
value
.
toUpperCase
(
)
)
{
return
{
valid
:
false
,
message
:
'Password must have at least one lowercase character'
,
}
;
}
if
(
value
.
search
(
/
[0-9]
/
)
<
0
)
{
return
{
valid
:
false
,
message
:
'Password must have at least one digit'
,
}
;
}
return
{
valid
:
true
}
;
}
;
const
res1
=
callback
(
)
.
validate
(
{
value
:
'123456'
,
options
:
{
callback
:
validatePassword
,
}
,
}
)
;
// res1.valid === false
const
res2
=
callback
(
)
.
validate
(
{
value
:
'not.contains.upper'
,
options
:
{
callback
:
validatePassword
,
}
,
}
)
;
// res2.valid === false
const
res3
=
callback
(
)
.
validate
(
{
value
:
'not'
,
options
:
{
callback
:
validatePassword
,
}
,
}
)
;
// res3.valid === false
const
res4
=
callback
(
)
.
validate
(
{
value
:
'v@@'
,
options
:
{
callback
:
validatePassword
,
}
,
}
)
;
// res4.valid === true