Get started with validatorjs CDN
MIT licensed
Validator.js is a versatile library for performing client-side form validation.
Tags:- javascript
- validator
- validation
- laravel
Stable version
Copied!
How to start using validatorjs CDN
// Import Validator.js from the CDN
const Validator = require('validatorjs');
// Define a custom validation rule for a simple email format
Validator.add('email', (value, attributes, params) => {
const regex = /^[\w-+\.]+\@[a-z\d\-]+(\.[a-z\d\-]+)*$/i;
return regex.test(value);
});
// Create a validation object with some rules
const rules = {
email: 'required|email'
};
// Create a data object with values to validate
const data = {
email: '[email protected]'
};
// Validate the data object against the rules
const validator = new Validator(data, rules);
// Check if validation passed
if (validator.passes()) {
console.log('Validation passed');
} else {
console.log('Validation failed:', validator.errors.all());
}
Copied!
Copied!