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

validate-kit

v1.0.5

Published

A lightweight JavaScript package for validating user input

Downloads

13

Readme

ValidateKit

ValidateKit is a utility class that provides various validation functions for common data types. These functions can be used to validate inputs such as email addresses, phone numbers, URLs, usernames, passwords, dates of birth, credit card numbers, postal codes, social security numbers, and even custom rules.

Table of Contents

Installation

To install the ValidateKit package, you can use npm. Open your terminal and run the following command:

npm install validate-kit

or

yarn add validate-kit

This command will install the validate-kit package and its dependencies into your project.

After installation, you can import and use the ValidateKit class in your JavaScript code as described in the Usage section.

Note: Make sure you have npm (Node Package Manager) installed on your machine before running the installation command.

Usage

To use the validation functions provided by ValidateKit, you can call them directly on the class. Here's an example of how to validate an email address:

import { ValidateKit } from 'validate-kit';

const email = '[email protected]';
const isValidEmail = ValidateKit.validateEmail(email);

console.log(isValidEmail); // Output: true

Feel free to replace email with your desired email address.

Functions

You can import the specific validation function you need or import the entire ValidateKit class to access all the validation functions.

validateEmail(email: string): boolean

This function validates whether the given email address is in a valid format. It checks if the email matches the pattern [email protected].

validatePhoneNumber(phoneNumber: string): boolean

This function validates whether the given phone number is in a valid format. It checks if the phone number consists of 10 to 15 digits.

validateURL(url: string): boolean

This function validates whether the given URL is in a valid format. It checks if the URL matches the pattern http(s)://domain.extension with an optional port and path.

validateUsername(username: string): boolean

This function validates whether the given username is in a valid format. It checks if the username consists of alphanumeric characters and is between 3 and 20 characters long.

validatePassword(password: string): boolean

This function validates whether the given password meets the required criteria. It checks if the password is at least 8 characters long and contains at least one uppercase letter, one lowercase letter, one digit, and one symbol.

validateConfirmPassword(password: string, confirmPassword: string): boolean

This function validates whether the given password and confirm password values match.

validateDateOfBirth(dateOfBirth: string): boolean

This function validates whether the given date of birth is in a valid format and represents a valid date. It checks if the date of birth matches the pattern YYYY-MM-DD and if it corresponds to a real date.

validateCreditCardNumber(creditCardNumber: string): boolean

This function validates whether the given credit card number is a valid number according to the Luhn algorithm checksum.

validatePostalCode(postalCode: string): boolean

This function validates whether the given postal code is in a valid format. It checks if the postal code matches the pattern of a 5-digit or 9-digit US ZIP code.

validateSocialSecurityNumber(ssn: string): boolean

This function validates whether the given social security number is in a valid format. It checks if the social security number consists of 9 digits.

validateCustomRule(value: any, rule: (value: any) => boolean): boolean

This function allows you to define and validate custom rules. It takes a value and a rule function as parameters. The rule function should return true if the value is valid according to your custom rule, or false otherwise.

Here are some examples of how to use the validateCustomRule function from ValidateKit to create custom validation rules:

Example 1: Validate that a string contains at least one uppercase letter:

import { ValidateKit } from 'validate-kit';

const value = 'Abcd1234';
const rule = (value) => /[A-Z]/.test(value);

const isValid = ValidateKit.validateCustomRule(value, rule);
console.log(isValid); // Output: true

Example 2: Validate that a number is greater than 10:

import { ValidateKit } from 'validate-kit';

const value = 15;
const rule = (value) => value > 10;

const isValid = ValidateKit.validateCustomRule(value, rule);
console.log(isValid); // Output: true

Example 3: Validate that an array contains at least 3 elements:

import { ValidateKit } from 'validate-kit';

const value = [1, 2, 3, 4, 5];
const rule = (value) => value.length >= 3;

const isValid = ValidateKit.validateCustomRule(value, rule);
console.log(isValid); // Output: true

License

This project is licensed under the MIT License.

Feel free to contribute to this project by opening issues or submitting pull requests.

If you encounter any issues or have any questions, please feel free to reach out.

Enjoy using ValidateKit for your validation needs!