Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

s-valid

Simple validator for strings

javascript, validator, validation, email validation, credit card validation, url validation, node, typescript

readme

s-valid.js

NPM version Dependencies build status NPM license Stability Test Coverage

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');

// simple validation
if (valid.email('email@test.com')) {
    // ...
}

// primary use case: server-side form validation
if (!valid.email(req.body.email)) {
    // return error...
}

Use it modularly if you prefer

var email = require('s-valid/email');

if (email('email@test.com')) {
    // ...
}

All Methods

Affirmative / Negatory
valid.affirmative('yes'); // true
valid.affirmative('y'); // true
valid.affirmative('on'); // true
valid.affirmative('true'); // true

valid.negatory('no'); // true
valid.negatory('n'); // true
valid.negatory('off'); // true
valid.negatory('false'); // true
Alpha (alphabetic) / Numeric / AlphaNumeric
valid.alpha('Foo'); // true
valid.alpha('Foo bar'); // false
valid.alpha('Test123'); // false

valid.numeric('123'); // true
valid.numeric('-123'); // true
valid.numeric('123px'); // false
valid.numeric('$123000'); // false

valid.alphaNumeric('Test123'); // true
valid.alphaNumeric('Tést'); // false
valid.alphaNumeric('Test 123'); // false
valid.alphaNumeric('Test_123'); // false
valid.alphaNumeric('Test-123'); // false
CreditCard

Note: valid.creditCard() is an alias for valid.card.generic()

valid.creditCard('4242424242424242'); // true (matches Visa regexp)
valid.creditCard('5610591081018250'); // true with no regexp match (Australian Bankcard)
valid.creditCard('1234123412341234'); // 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)
valid.card.amex('371449635398431'); // true
valid.card.amex('4242424242424242'); // false (is Visa)
Email
valid.email('email@test.com'); // true
valid.email('email@test'); // false
Social Security Number (USA)
valid.socialSecurity('078-05-1120'); // true (from Wikipedia)
valid.socialSecurity('078-00-1120'); //false
valid.socialSecurity('078051120'); // 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)

valid.value('123'); // true
valid.value('-123'); // true
valid.value('#000000'); // true
valid.value('123px'); // true
valid.value('$123,000.00'); // true
valid.value('test'); // false
valid.value('Infinity'); // false
URL
valid.url('http://test.com'); // true
valid.url('https://test.com'); // true
valid.url('https://test.com:3000'); // true -- works with port numbers
valid.url('http://4.35.153.221'); // true -- IP addresses are valid URLs
valid.url('http://300.35.153.221'); // false -- invalid IP addresses fail
valid.url('http:/test.com'); // false
Zip code
valid.zipCode('89052'); // true
valid.zipCode('89052-6589'); // false
valid.zipCode('890526589'); // false
Zip code + 4

Additional 4-digit code is optional and must be separated by a hyphen

valid.zipCodeLong('89052'); // true
valid.zipCodeLong('89052-6589'); // true
valid.zipCodeLong('890526589'); // 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

changelog

s-valid changelog

1.4.0

  • use typescript
  • minor refactor
  • use ava/nyc instead of mocha/istanbul for tests

1.3.0

  • convert to es6 (mainly just let/const)
  • replace blanket with istanbul for code coverage
  • replace jshint with eslint
  • add jscs for code style consistency linting
  • remove gulp--just npm scripts now
  • fix bug where parseInt required a radix

1.2.0

  • made all methods modular
    • eg. var email = require('s-valid').email to only add the email method
  • methods should throw (not just return false) when passed arguments of wrong type, so now they do.
  • speed improvements to valid.creditCard by removing unnecessary regexp tests

1.1.0

  • distinct mastercard and visa regular expressions
    • removed mastercardVisa

1.0.0

  • no more number methods
    • zip takes strings (not numbers)
    • creditCard and card.{type} take strings
    • removed integer method (too simple)

0.6.0

  • breaking changes
    • renamed valueString to value
    • renamed numberString to numeric
  • other changes
    • added alpha method

0.5.0

  • breaking changes
    • renamed numberStringLoose to valueString
    • removed number methods positive and negative

0.4.0

  • breaking changes. removed the following string methods:
    • empty
    • notEmpty
    • space
    • length
    • maxChars
    • minChars

0.3.0

  • breaking changes to credit card validation
    • valid.creditCard is now an alias (shortcut) for valid.card.generic
    • all specific creditCard methods are now in the card object rather than creditCard object (eg. use valid.card.amex(n))

0.2.0

  • breaking changes to credit card validation
    • the former valid.creditCard method is now valid.creditCard.generic and allows for more card types
    • specific card regexps have been added and can be used as follows:
      • valid.creditCard.amex(number); // returns true or false
      • valid.creditCard.maestro(number); // returns true or false
      • etc. works with: visa, mastercard, amex, maestro, jcb, unionpay, discover, solo, carteBlanche, dinersClub, and lasercard
      • Note that visa and mastercard share a regexp and are each aliases of creditCard.mastercardVisa(number);