input-validators-js
v1.0.8
Published
Input validation functions for common front-end application use.
Downloads
14
Maintainers
Readme
input-validators-js
A simple npm-package for validating user input like email
, password
-strength, date
-format and so on.
Installation
With npm
:
npm i input-validators-js
With yarn
:
yarn add input-validators-js
Including input-validators-js
in your project:
const validate = require("input-validators-js");
...
Validating email formatting
validate.validateEmail(emailString);
This will return true
if the string passes the email-regex and false
if not.
Validating names
validate.validateName(nameString);
This will return true
if the string passes the name-regex and false
if not.
Validating password strength
This function is used to check password strength based on characters present in the passed string.
Strong password strength regex
^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})
Medium password strength regex
^(((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9])))(?=.{6,})
Passing the password through
validate.validatePasswordStrength(passwordString);
will return one of three possible objects:
passwordAccepptable: true, passwordStrength: "strong"
passwordAccepptable: true, passwordStrength: "medium"
passwordAccepptable: false, passwordStrength: "weak"
Validating date-formatting DD/MM/YYYY
validate.validateDateFormatDayMonthYear(dateString);
This will return true
if the string passes the date-regex and false
if not.
Validating date-formatting YYYY/MM/DD
validate.validateDateFormatYearMonthDay(dateString, format);
Accepts two parameters:
dateString
- This is the date which is being validated.
format
- number
0, 1, or 2
which changes formatting regex accordingly:
0
- YYYY/M/D and YYYY.M.D1
- YYYY-MM-DD and YYYY-M-D2
- YYYY-MM-DD
Will return
true
or false
if dateString
passes regex.
Validating type string
validate.validateTypeString(string);
This will return true
if the object passed is a string and false
if not.
Validating type number
validate.validateTypeNumber(number);
This will return true
if the object passed is a number and false
if not.