1.0.5 • Published 5 years ago
newsl-validator v1.0.5
newsl-validator
This module is meant to validate some fields of a basic newsletter form. The idea behind this is that to apply a validation criteria to the value, that is a string, of the fields of the form. It is provided a default validation for the this default fields :
- Name, which is validated with a regular expression that accept only characters and discard white space and @#?|!
- Surname , which is validated with a regular expression that accept only characters and discard @#?|!
- Email, which is validated with a regular expression that accept only example@domain.ext format.
- Birth date, which is validated with a regular expression that accept DD/MM/YYYY format.
Installation
To use this module install it with :
npm install newsl-validator
Usage
The module expose a single method called validator which return true or false after validating the field given in input, which is an object with these properties :
var validatorModule = require('newsl-validator');
var nameField = {
content : 'Alex',
type : 'NAME'
}
var res = validatorModule.validator(nameField); //true
The property type is used to fire the default validation criteria and can be valued as :
NAME,SURNAME,EMAIL,BIRTH_DATE
If you wanna apply a custom criteria to a default field or a new field you can use the criteria property :
var validatorModule = require('newsl-validator');
surnameField = {
content : 'Clinton',
type : 'SURNAME',
criteria : function(content){
return /^[^0-9 @#?|!]+$/.test(content) && content.length < 2;
}
}
var res = validatorModule.validator(nameField); //false