Documentation
# How to install:
via npm:
npm install rj-validate --save
via a CDN:
Copy the following script tag into the bottom of the page:
<script src="https://cdn.jsdelivr.net/npm/rj-validate@1.5.0/dist/rj.min.js"></script>
# Validating an input:
var rj = require('rj-validate/dist/rj');
rj().validate('abc123', {
required: true
});
// output:
// Object {message: "all tests pass", valid: true}
See the Pen rj-validate basic example by Robert Anderson (@rander) on CodePen.
The validate() and test() methods are interchangeable:
var rj = require('rj-validate/dist/rj');
rj().test('abc123', {
required: true
});
// output:
// Object {message: "all tests pass", valid: true}
The validate() method takes three arguments:
rj().validate(
[ the input to validate (either a string or a number) ],
[ the rules to test the input against (object) ],
[ the name of the variable (a string) (optional) ]
)
The third argument is only used for displaying contextualized error messages.
The output of the validate() method is an object with the following structure:
{
valid: (true|false),
message: (the specific error message)
}
# Custom error messages:
If you would like to replace the default error message with your own, you can pass that message into the rules argument:
rj().test('', {
required: true,
required_msg: "That's a failure"
});
// output:
// Object {message: "That's a failure", valid: false}
For each rule that you'd like to have a custom error message, add a [rule]_msg option to the rules argument, as shown above.
# Available validation options:
rules = {
required: (true|false),
same: {name: (string), value: (string|number|boolean)},
different: {name: (string), value: (string|number|boolean)},
min: (integer|false),
max: (integer|false),
alpha: (true|false),
name: (true|false|'no-accents'),
alphanumeric: (true|false),
numeric: (true|false|'number'|'string'),
number: (true|false),
string: (true|false),
date: (true|false|'yyyy-mm-dd'|'mm/dd/yyyy'|'mm/dd/yy'),
datetime: (true|false|'yyyy-mm-dd hh:mm:ss'
|'mm/dd/yyyy hh:mm:ss'|'mm/dd/yy hh:mm:ss'),
email: (true|false),
po_box: (true|false),
not_po_box: (true|false),
in: (array|object|false),
not_in: (array|object|false),
before: (string|Date|false),
before_or: (string|Date|false),
after: (string|Date|false),
after_or: (string|Date|false),
zip: (true|false|'usa-5'|'usa-9')
}
Note that a [rule]:false will always be considered valid. This option exists solely to help dynamically specify rules at runtime.
# isValid for faster validation:
In some scenarios, you may not be interested in the specific error message and you only want to know if the request passes all validations. In those situations, you can use isValid()
rj().isValid('1987-10-01', {
required: true,
date: 'yyyy-mm-dd',
before: '1980-01-01'
});
// output:
// false
See the Pen rj-validate isValid example by Robert Anderson (@rander) on CodePen.
# Custom validation rules:
If you need to use a validation rule that isn't in the list above, you can create a custom rule. Here is an example of a custom rule that checks if the first letter in a line of text is capitalized:
function startsWithCapital(input) {
return input[0] === input[0].toUpperCase();
}
var rj = require('rj-validate/dist/rj');
rj().test('the quick brown fox jumps over the lazy dog', {
required: true,
min: 4,
custom: [{
test: function(input) {
return startsWithCapital(input);
},
error_msg: 'Please start your sentences with a capital letter.'
}]
});
// output:
// Object {message: "Please start your sentences with a capital letter.", valid: false}
Custom rules give you a high degree of flexibility, while still working within the framework of a validation library. You can even use the custom validations array to reorganize which validations run before others:
See the Pen rj-validate reordered validations by Robert Anderson (@rander) on CodePen.
# Validating multiple inputs:
You can also validate multiple inputs at the same time with the validateAll() method:
See the Pen rj-validate validateAll example by Robert Anderson (@rander) on CodePen.
# More Examples:
var rj = require('rj-validate/dist/rj')();
rj.test('abc123', {
required: true
});
// output:
// Object {message: "all tests pass", valid: true}
rj.test('', {
required: true
});
// output:
// Object {message: "Required", valid: false}
rj.test('', {
required: true
}, 'first name');
// output:
// Object {message: "first name is required", valid: false}
rj.test('', {
required: true,
required_msg: "That's a failure"
});
// output:
// Object {message: "That's a failure", valid: false}
rj.validate('foobar', {
required: true,
required_msg: 'Profile name is definitely required',
min: 7,
min_msg: "Hold on, that profile name isn't long enough"
});
// output:
// Object {message: "Hold on, that profile name isn't long enough", valid: false}
# Changelog:
v1.6.3
Bugfixes
v1.6.0
Added a name validation rule, which allows for additional characters not allowed in the alpha rule
v1.5.0
Added a zip code validation rule. The validateAll method now returns a summarized 'valid' output parameter, in addition to the detailed breakdown of the validity of each input parameter
v1.4.0
The is_alpha rule now works correctly for input strings with spaces
v1.3.2
Moved the documentation out of the readme file
v1.3.1
Fixed a bug with using the library as a CDN
v1.2.0
Fixed a bug with module exports and using the library as a CDN
v1.1.0
The library now exports a function instead of a object. The readme file and tests have been updated to reflect this change.