callback validator

Check if the input value passes a callback method

Options

Using with form field
The HTML attributes are used to set the validator options via the Declarative plugin
(* denotes a required option)
NameHTML attributeTypeDescription
callback*data-fv-callback___callbackFunctionThe callback method
messagedata-fv-callback___messageStringThe error message
The callback method must follow the format below:
function(input) {
// input is an object of
// {
// value: The field value,
// options: The callback validator options
// }
// Check the field validity
return true; // or false
}
If you want to return a dynamic message, the callback function must return an object containing the 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'
};
}
Look at the Alias plugin if you want to have multiple callbacks on the same field
Using the ES6 module
// You might need to change the importing path
import { callback } from '/vendors/@form-validation/cjs/validator-callback';
const result = callback().validate({
value: ...,
options: {
callback: ...,
},
},
});
/*
result is an object of
{
valid: true or false,
message: The error message
}
*/
Using the npm package
  • Install the validator package:
$ npm install @form-validation/validator-callback
  • Use the callback validator:
import { callback } from '@form-validation/validator-callback';
const result = callback().validate({
value: ...,
options: {
callback: ...,
},
});

Basic example

In the following form, user is asked to enter a correct answer of simple operation which is generated randomly.
callback validator

NPM package example

The following snippet shows how to use the callback validator with ES6 module to check if a password is strong or weak:
// You might need to change the importing path
import callback from '/vendors/@form-validation/cjs/validator-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@CONTAIN@digit',
options: {
callback: validatePassword,
},
});
// res3.valid === false
const res4 = callback().validate({
value: 'v@l1dP@@',
options: {
callback: validatePassword,
},
});
// res4.valid === true

See also

Changelog

v2.0.0
  • Add the npm package