A javascript validation package motivated by laravel default validator.
npm install easy-js-validator``javascript`
const { Validator } = require('easy-js-validator');
const validator = new Validator({
name: "Alice",
age: "22",
isMarried: false
});
const rules = {
name: 'required|between:5,50|alpha',
age: 'between:20,30',
isMarried: 'boolean'
};
const errors = validator.validate(rules);
`javascript``
const messages = {
name: {
required: 'Please enter :attribute, it is required.',
between: 'The lenght should be between 20 to 30 characters.',
}
};
const errors = validator.validate(rules,messages);
Note: ":attribute" will be replace by the validating input name.
The field under validation must be present in the input data and not empty. A field is "empty" if it meets one of the following criteria:
- The value is null.
- The value is an empty string.
- The value is an empty array or empty Countable object.
- The value is an uploaded file with no path.
The field under validation must be an integer.
The field under validation must be an decimal number. Both minDecimalPlaces and maxDecimalPlaces are optional fields. If neither is provided, the function will only check if the given value is a decimal. If only minDecimalPlaces is provided, it will validate that the value has exactly the specified number of decimal places. If both minDecimalPlaces and maxDecimalPlaces are provided, it will ensure the value has a number of decimal places within the specified range.
The field under validation must have a minimum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.
The field under validation must have a maximum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.
The field under validation must have a size between the given min and max (inclusive). Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.
The field under validation must be formatted as an email address.
The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".
The field under validation must be a valid.
The field under validation must be included in the given list of values.
The field under validation must not be included in the given list of values.
The field under validation must not be a start with either http:// or https://.
The field under validation must contains only English lowercase(a-z) and upprcase(A-Z) alphabets.
The field under validation must contains only English lowercase(a-z), upprcase(A-Z) alphabets and 0-9.
The field under validation must contains only English lowercase(a-z), upprcase(A-Z) alphabets and '_'.
The field under validation must contains only English lowercase(a-z), upprcase(A-Z) alphabets, 0-9 and '_'.
The given field must match the field under validation.
The field under validation must match the given regular expression.
Eg. regex:^[a-zA-Z]+$ //It will check if given iput contains lettrs between a-z and A-Z.