npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

s-valid

v1.4.0

Published

Simple validator for strings

Downloads

157

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 protected]')) {
	// ...
}

// 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 protected]')) {
	// ...
}

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 protected]'); // 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