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

common-validations

v1.0.2

Published

Simple js library to common string validation

Downloads

5

Readme

Common Validations Library

NPM Version NPM License NPM Downloads NPM Unpacked Size

common-validations is a lightweight library providing a suite of utility functions to validate various data types. The library includes functions for validating URLs, email addresses, phone numbers, IPv4 addresses, MAC addresses, ISO 8601 dates, credit card numbers, and strong passwords. Each function employs regular expressions to validate the input string against the corresponding data type. The library is designed to be simple, efficient, and easy to use, making it ideal for validating user input in web applications, APIs, and other software projects.

Installation

npm install common-validations

Usage

Import the necessary functions from the library and use them to validate your data:

import Validator, {
isURL,
isEmail,
isPhone,
isIPv4,
isMAC,
isISODate,
isCreditCard,
isStrongPassword,
} from "common-validations";

console.log(Validator.isURL("https://example.com")); //true
console.log(isEmail("[email protected]")); //true
console.log(isPhone("+1-800-555-1234")); //true
console.log(isIPv4("192.168.1.1")); //true
console.log(isMAC("00:1A:2B:3C:4D:5E")); //true
console.log(isISODate("2023-04-05")); //true
console.log(isCreditCard("4111111111111111")); //true
console.log(isStrongPassword("Aa1@strong")); //true

API Reference

The Validator object is the primary entry point for the library, encompassing validation functions for various data types.

Each function accepts a string as input and returns a boolean indicating whether the input meets the validation criteria.

Detailed Documentation

Validator Object

The Validator object includes the following functions:

  • isURL - isURL(url: string): boolean
  • isEmail - isEmail(email: string): boolean
  • isPhone - isPhone(phoneNum: string): boolean
  • isIPv4 - isIPv4(ip: string): boolean
  • isMAC - isMAC(mac: string): boolean
  • isISODate - isISODate(date: string): boolean
  • isCreditCard - isCreditCard(card: string): boolean
  • isStrongPassword - isStrongPassword(password: string): boolean

Example

You can use the default import to access the Validator object and its functions:

import Validator from "common-validations";

console.log(Validator.isURL("https://example.com")); //true
console.log(Validator.isURL("invalid-url")); //false

Or you can import individual functions directly:

import { isURL } from "common-validations";
console.log(isURL("https://example.com")); //true

isURL

Validates if the provided string is a valid URL.

  • Parameter: url - The URL string to validate.
  • Returns: true if the string is a valid URL, false otherwise.
Example isURL('https://example.com'); // true
isURL('invalid-url'); // false 

isEmail

Validates if the provided string is a valid email address.

  • Parameter: email - The email address string to validate.
  • Returns: true if the string is a valid email address, false otherwise.
isEmail('[email protected]'); // true
isEmail('user@example'); // false

isPhone

Validates if the provided string is a valid phone number.

  • Parameter: phoneNum - The phone number string to validate.
  • Returns: true if the string is a valid phone number, false otherwise.
isPhone('+1-800-555-1234'); //true
isPhone('123-abc-7890'); //false

isIPv4

Validates if the provided string is a valid IPv4 address.

  • Parameter: ip - The IPv4 address string to validate.
  • Returns: true if the string is a valid IPv4 address, false otherwise.
isIPv4('192.168.1.1'); //true
isIPv4('999.999.999.999'); //false

isMAC

Validates if the provided string is a valid MAC address.

  • Parameter: mac - The MAC address string to validate.
  • Returns: true if the string is a valid MAC address, false otherwise.
isMAC('00:1A:2B:3C:4D:5E'); //true
isMAC('00-1A-2B-3C-4D-5E-GH'); //false

isISODate

Validates if the provided string is a valid ISO 8601 date.

  • Parameter: date - The ISO 8601 date string to validate.
  • Returns: true if the string is a valid ISO 8601 date, false otherwise.
isISODate('2023-04-05'); //true
isISODate('2023-13-01'); //false

isCreditCard

Validates if the provided string is a valid credit card number.

  • Parameter: card - The credit card number string to validate.
  • Returns: true if the string is a valid credit card number, false otherwise.
isCreditCard('4111111111111111'); //true
isCreditCard('1234567812345678'); //false

isStrongPassword

Validates if the provided string is a strong password.

  • Parameter: password - The password string to validate.
  • Returns: true if the password meets the strong password criteria, false otherwise.

A strong password typically includes a mix of:

  • At least one lowercase letter
  • At least one uppercase letter
  • At least one numeric digit
  • At least one special character from [@ $ ! % * ? &]
  • A minimum length of 8 characters
isStrongPassword('Aa1@strong'); //true 
isStrongPassword('weakpass'); //false

Contributing

Contributions are welcome! Please open an issue or submit a pull request to contribute.

License

This library is licensed under the MIT License. See the LICENSE file for details.

Happy validating!