input-validate
v2.0.0
Published
Easy and efficient serverside validation plugin. Create CUSTOM validation rules to meet your requirements. Also there are pre-defined regex validators for quick use.
Downloads
50
Maintainers
Readme
input-validate
A bunch of pure functions with regular expressions, for input validation purpose (when you are not using any type checking library/framework). A couple of predefined check-functions and a - flexible CUSTOM test function to meet any other random needs!
INSTALL
npm i -S input-validate
HOW TO USE
var TEST = require('input-validate');
if ( TEST.strictNumbers(123) ) {
console.log("It is a valid number.")
}
Use Table of CONTENT to navigate through the page
- isNumber( )
- strictNumbers( )
- isString( )
- strictAlphabets( )
- alphanumeric( )
- quickPassword( )
- isEmail( )
- isPersonName( )
- isCountry( )
- customOr( ) : A flexible function to implements custom test with 'OR' condition
- customAnd( ): A flexible function to implements custom test with 'AND' condition
- ES6 Import
QUICK TESTS
isNumber(input) checks if input is a valid number uses typeof
{Table of Content}
// --- usage ---
if ( TEST.isNumber(123) ) {
console.log("It is a valid number.")
}
strictNumbers(input, true by default) checks if input has valid numbers {Table of Content}
// --- usage ---
if ( TEST.strictNumbers(123) ) {
console.log("It is a valid number.")
}
// --- tests ---
√ should return FALSE - strictNumbers('1234')
√ should return TRUE - strictNumbers('1234', checkType = false)
√ should return TRUE - strictNumbers(1234)
√ should return TRUE - strictNumbers(1234, checkType = false)
√ should return FALSE - strictNumbers(`123`)
√ should return TRUE - strictNumbers(`123`, checkType = false)
√ should return FALSE - strictNumbers()
√ should return FALSE - strictNumbers(null)
isString(input) Checks if input is a valid string uses typeof
{Table of Content}
// --- usage ---
if ( TEST.isString("abc") ) {
console.log("It is a valid number.")
}
strictAlphabets(input) Checks if input contains only alphabets {Table of Content}
// --- usage ---
if ( TEST.strictAlphabets("abc") ) {
console.log("Contains only alphabets.")
}
// --- tests ---
√ should return TRUE - strictAlphabets('abc')
√ should return TRUE - strictAlphabets('abcABC')
√ should return FALSE - strictAlphabets(123)
√ should return FALSE - strictAlphabets('1234')
√ should return FALSE - strictAlphabets(`123`)
√ should return FALSE - strictAlphabets('123abc')
√ should return FALSE - strictAlphabets()
√ should return FALSE - strictAlphabets(null)
alphanumeric(input, false by default) This function takes 2 arguments. first one is input and second one is "strictMode" as boolean. By default strictMode
= false
, is that case the validator returns true if the input contains either alphabets or number or both. But when you make the strictMode
= true
, it checks for -the input must contain alphabets and numbers. Note -no space or symbols are tolarated. {Table of Content}
--- usage ---
if ( TEST.alphanumeric("abc") ) {
console.log("Valid alphanumeric in strictMode = default|false.")
}
if ( TEST.alphanumeric("abc", true) ) {
} else {
console.log("InValid alphanumeric in strictMode = true.")
}
if ( TEST.alphanumeric("abc123") ) {
console.log("Valid alphanumeric")
}
--- tests ---
√ should return TRUE alphanumeric('abc123ABC')
√ should return TRUE alphanumeric('abc')
√ should return FALSE alphanumeric('abc', strict = true)
√ should return TRUE alphanumeric(123)
√ should return FALSE alphanumeric(123, strict = true)
√ should return FALSE alphanumeric('123 abc')
√ should return FALSE alphanumeric('123@abc')
quickPassword(input) A quick password check with some ground rules: {Table of Content}
--- RULE ---
1. Must contain at least 1 number and 1 letter
2. Must have atleast 8 characters
3. May contain blank space
4. May contain any of these characters: ._@#$%
--- usage ---
if ( TEST.quickPassword("123abc_XYZ") ) {
console.log("Valid password")
}
--- tests ---
√ should return FALSE quickPassword('1234567')
√ should return FALSE quickPassword('123456789')
√ should return FALSE quickPassword('abcDEFghij')
√ should return FALSE quickPassword('12345 6789')
√ should return TRUE quickPassword('12345 A@ 6789')
√ should return TRUE quickPassword('123abc_XYZ')
√ should return TRUE quickPassword('1234abcXYZ')
isEmail(input) Checks for valid email address {Table of Content}
--- usage ---
if ( TEST.isEmail("[email protected]") ) {
console.log("Valid email address")
}
--- tests ---
√ should return TRUE isEmail('[email protected]')
√ should return TRUE isEmail('[email protected]')
√ should return TRUE isEmail('[email protected]')
√ should return TRUE isEmail('[email protected]')
√ should return FALSE isEmail('val [email protected]')
√ should return FALSE isEmail('val/[email protected]')
√ should return FALSE isEmail(' [email protected]')
√ should return FALSE isEmail(' [email protected]')
isPersonName(input) Checks for a possible valid full name of a person including salutation checks like ["Mr", "Master", "Sir", "Dr", "Miss", "Ms", "Mrs", "Prof", "Rev", "Capt", "Maj", "Dj", "Pvt" ] {Table of Content}
--- usage ---
if ( TEST.isPersonName("John Doe") ) {
console.log("Valid person name")
}
--- tests ---
√ should return FALSE isPersonName('Do')
√ should return TRUE isPersonName('Doe')
√ should return TRUE isPersonName('John Doe')
√ should return TRUE isPersonName('Mr. John Doe')
√ should return TRUE isPersonName('Mrs, Johane Doe')
√ should return FALSE isPersonName('Etc. John')
√ should return FALSE isPersonName('Mr. J')
√ should return FALSE isPersonName('$Dollar Sign')
√ should return FALSE isPersonName('John_Doe')
isCountry(input) Checks if given input is a valid country name. (Checks trough a list of more than 210 countries) {Table of Content}
--- usage ---
if ( TEST.isCountry("India") ) {
console.log("Valid country name")
}
--- tests ---
√ should return TRUE isCountry('Panama')
√ should return TRUE isCountry('el salvador')
√ should return TRUE isCountry('st lucia')
√ should return TRUE isCountry('St. Lucia')
√ should return TRUE isCountry('Saint Lucia')
√ should return TRUE isCountry('US')
√ should return TRUE isCountry('united states')
CUSTOM rule creation
customOr: function(input, {rules}, flag){
//Implementation
}
Parameters of custom validation
1. `input`: (Required) `String`. Is the input to test against the `rules`
2. `rules`: (Optional) `Object`. Validation configuration
3. `flag`: (Optional) `String`. This method uses [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) to validate inputs. This parameter is the flag of the function. : new RegExp('-----', `flag`)
CUSTOM Rule
The second parameter of the function customOr("",allow_rules)
is used to create custom validation.
rules = {
alphabets: true,
numbers: '0-9',
spaces: false,
symbols: '',
minlength: '',
maxlength: '',
};
alphabets: can be boolean or Object. When boolean: true - means, it will consider upper & lower case alphabets. When object: { uppercase: true (will enable uppercase), lowercase: true (will enable lowercase alphabets) } When none is passed - alphabets: true (by default) is considered.
numbers: can be string range or boolean When boolean: true - means, it will consider range from 0 to 9 When string: Should be valid range in format- "[min]-[max]" line "0-9" When none is passed - numbers: '0-9'(by default) is considered
spaces: must be passed as boolean, by default its false. i.e no spca is tolatated.
symbols: should be passed as string like '.-_'
minlength and maxlength: a valid digit should be passed as number/string.
Custom rule Example:
var TEST = require('input-validate');
var str = "sample @123";
var rule = {spaces: true, symbols: "$#@"}
var result = TEST.customOr(str, rule); // False
Whats happening here is
1. We want to validate the string "str" = "sample @123".
2. Create a "rule" = {spaces: true, symbols: "$#@"}. The string "str" may contain blank space and no special symbol other than "$#@" these.
3. Execute the TEST. Validate "str" against the "rule".
4. Value of "result" is true;
--- test ---
Testing customOr configurable test function
√ should return FALSE - customOr('abcABC 123', {spaces: false})
√ should return TRUE - customOr('abcABC123', {spaces: false})
√ should return FALSE - customOr('abc123', { alphabets: false, space: false }) - No uppercase, no space
√ should return TRUE - customOr('abcABC123', { alphabets: true, space: false }) - No uppercase, no space
√ should return FALSE - customOr('abcABC123', { alphabets: { uppercase: false, }, space: false }) - No uppercase, no space
√ should return FALSE - customOr('abcABC123', { alphabets: { lowercase: false, }, space: false }) - No lowercase, no space
√ should return TRUE - customOr('ABC123', { alphabets: { lowercase: false, }, space: false }) - No lowercase, no space
√ should return TRUE - customOr('ABC123', { alphabets: { lowercase: false, }, space: false }) - No lowercase, no space
√ should return FALSE - customOr('ABC@123') - symbols not configured
√ should return TRUE - customOr('ABC@123', {symbols: '@'}) - symbols @ configured
√ should return TRUE - customOr('ABC@123', {symbols: '@$'}) - symbols @ and $ configured
√ should return TRUE - customOr('"ABC"123', {symbols: '"'}) - symbols " is configured, (use escape char or not)
√ should return FALSE - customOr('123', {minlength: 4}) - symbols not configured
√ should return TRUE - customOr('12345', {minlength: 4}) - symbols not configured
√ should return FALSE - customOr('123456', {minlength: 4, maxlength: 5}) - symbols not configured
√ should return TRUE - customOr('123456', {minlength: 4, maxlength: 5}) - symbols not configured
customAnd()
It is same as customOr()
defined above. The only difference between these two custom is -
- In case of
customOr()
: MAY contain alphabets or number or both. - In case of
customAnd()
: MUST contain atleast 1 alphabets or atleast 1 number or atleast 1 of both - alphabets and numbers (depending on rules defined)
ES6 Import
If your application supports ES6 then you can also import specific tests from es6
directory like this:
import {isString, strictNumbers, customOr, } from 'input-validate/es6';
if( isString("234fff") ) {
// do something
}