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 = ; // simple validationif valid // ... // primary use case: server-side form validationif !valid // return error...
Use it modularly if you prefer
var email = ; if // ...
All Methods
Affirmative / Negatory
valid; // truevalid; // truevalid; // truevalid; // true valid; // truevalid; // truevalid; // truevalid; // true
Alpha (alphabetic) / Numeric / AlphaNumeric
valid; // truevalid; // falsevalid; // false valid; // truevalid; // truevalid; // falsevalid; // false valid; // truevalid; // falsevalid; // falsevalid; // falsevalid; // false
CreditCard
Note: valid.creditCard()
is an alias for valid.card.generic()
valid; // true (matches Visa regexp)valid; // true with no regexp match (Australian Bankcard)valid; // false
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
)
validcard; // truevalidcard; // false (is Visa)
valid; // truevalid; // false
Social Security Number (USA)
valid; // true (from Wikipedia)valid; //falsevalid; // false
Value string
Similar to numeric
, but less restrictive. Passes for $ (or any non-number first character), commas, and units (such as 12px or 38BTC)
validvalue'123'; // truevalidvalue'-123'; // truevalidvalue'#000000'; // truevalidvalue'123px'; // truevalidvalue'$123,000.00'; // truevalidvalue'test'; // falsevalidvalue'Infinity'; // false
URL
valid; // truevalid; // truevalid; // true -- works with port numbersvalid; // true -- IP addresses are valid URLsvalid; // false -- invalid IP addresses failvalid; // false
Zip code
valid; // truevalid; // falsevalid; // false
Zip code + 4
Additional 4-digit code is optional and must be separated by a hyphen
valid; // truevalid; // truevalid; // false
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