The most versatile validator for Node JS and web browsers.
var checkval = require('checkval');
<script type="text/javascript" src="checkval.min.js"></script>
Returning boolean:
if ( !checkval('email@example.com').email().check() ) {
console.error('invalid data');
}
Returning boolean (multiple validations):
if ( !checkval().
add('456457', 'id').numeric().len(5, 10).
add('email@example.com').email().check() ) {
console.error('invalid data');
}
Throwing an error:
try {
checkval().
add('456457', 'id').numeric().len(5, 10).
add('email@example.com', 'email').email().
throw();
} catch (err) {
console.error(err);
}
Getting all errors:
var errors = checkval().
add('456457', 'id').numeric().len(5, 10).
add('email@example.com').email().
errors();
while ( errors.length > 0 ) {
console.error(errors.pop());
}
- alpha() : check if the value contains only letters (a-zA-Z).
- alphanumeric() : check if the value contains only letters and numbers (a-zA-Z0-9).
- email() : check if the value is an email.
- len(min[, max]) : check if the value's length falls in a range.
- notNull() : check if the value is not null.
- null() : check if the value is null.
- integer() : check if the value is an integer (e.g. 345, -81)
- positiveInt() : check if the value is a positive integer (e.g. 345)
- numeric() : check if the string contains only a valid integer or decimal number (0-9).
- date() : check if the string is a valid date.
- regex(regex[, msg]) : returns true if the value matches the comparison. e.g.:
checkval().add("test").regex(/^test$/).check()
- uuid() : check if the value is a UUID (version 3, 4 or 5).
- bool() : check if the value is a boolean (strict)
- array() : check if the value is in a valid array.
- inArray(values) : check if the value is in a array of allowed values.
To run the test suite, first invoke the following command within the project folder, installing the development dependencies:
$ npm install
Then run the tests:
$ npm test