s-valid.js
Simple modular string validator
For common tests (credit cards, urls, email addresses, ...)
- Dependency-free
- Tested on node & iojs
- Uses es6, so transpile for browser use
Note: Strings only!
This module simplifies validation that requires regular expressions or multiple steps. It will not include anything that is already simple to calculate and reason about, such as string length.
Installation
npm install --save s-valid
Usage
var valid = require('s-valid');
if (valid.email('email@test.com')) {
}
if (!valid.email(req.body.email)) {
}
Use it modularly if you prefer
var email = require('s-valid/email');
if (email('email@test.com')) {
}
All Methods
Affirmative / Negatory
valid.affirmative('yes');
valid.affirmative('y');
valid.affirmative('on');
valid.affirmative('true');
valid.negatory('no');
valid.negatory('n');
valid.negatory('off');
valid.negatory('false');
Alpha (alphabetic) / Numeric / AlphaNumeric
valid.alpha('Foo');
valid.alpha('Foo bar');
valid.alpha('Test123');
valid.numeric('123');
valid.numeric('-123');
valid.numeric('123px');
valid.numeric('$123000');
valid.alphaNumeric('Test123');
valid.alphaNumeric('Tést');
valid.alphaNumeric('Test 123');
valid.alphaNumeric('Test_123');
valid.alphaNumeric('Test-123');
CreditCard
Note: valid.creditCard()
is an alias for valid.card.generic()
valid.creditCard('4242424242424242');
valid.creditCard('5610591081018250');
valid.creditCard('1234123412341234');
Card.{type}
- Amex (
amex
) - Carte Blanche (
carteBlanche
) - Diners Club (
dinersClub
) - Discover (
discover
) - JCB (
jcb
) - Lasercard (
lasercard
) - Maestro (
maestro
) - Mastercard (
mastercard
) - Solo & Switch (
solo
) - Unionpay (
unionpay
) - Visa (
visa
)
valid.card.amex('371449635398431');
valid.card.amex('4242424242424242');
Email
valid.email('email@test.com');
valid.email('email@test');
Social Security Number (USA)
valid.socialSecurity('078-05-1120');
valid.socialSecurity('078-00-1120');
valid.socialSecurity('078051120');
Value string
Similar to numeric
, but less restrictive. Passes for $ (or any non-number first character), commas, and units (such as 12px or 38BTC)
valid.value('123');
valid.value('-123');
valid.value('#000000');
valid.value('123px');
valid.value('$123,000.00');
valid.value('test');
valid.value('Infinity');
URL
valid.url('http://test.com');
valid.url('https://test.com');
valid.url('https://test.com:3000');
valid.url('http://4.35.153.221');
valid.url('http://300.35.153.221');
valid.url('http:/test.com');
Zip code
valid.zipCode('89052');
valid.zipCode('89052-6589');
valid.zipCode('890526589');
Zip code + 4
Additional 4-digit code is optional and must be separated by a hyphen
valid.zipCodeLong('89052');
valid.zipCodeLong('89052-6589');
valid.zipCodeLong('890526589');
Additional notes
is.js provided some of the regular expressions used behind-the-scenes, however the following improvements have been made to them:
- URLs can include the port number and be IP addresses without failing
- Affirmative string values can include any capitalization
- "on" is also considered affirmative (useful for capturing checkbox values in POST requests)
- Negatory string values ("off", "no", "false") have been added
- More credit card types will pass validation