regex-validify
v3.0.0
Published
Your best friend when it comes to validation ๐!
Downloads
204
Maintainers
Readme
๐ ๏ธ Regex Validify ๐ก๏ธ
๐ Table of Contents
Installation
To install your best friend simply run the command:
npm i regex-validify
Description
Regex Validify is a library using which you can very easily check inputs without having the hassle of remembering complex regex patterns. It could be your ultimate friend when it comes to checking users input ๐ช๐ผ.
Features
This library includes the following validation functions:
- checkEmail: Validates email addresses.
- checkDate: Validates date formats.
- checkPhoneNumber: Validates phone numbers.
- checkURL: Validates URLs.
- checkOnlyLetters: Checks if a string contains only letters.
- checkPassword: Validates password strength.
- checkOnlyIntegers: Checks if a string contains only integers.
- checkCreditCard: Validates credit card numbers.
- checkUsername: Validates usernames.
- checkIPv4: Validates IPv4 addresses.
- checkHexColor: Validates hexadecimal color codes.
- checkMACAddress: Validates MAC addresses.
- CustomRegex: Allows users to define their own regex validation.
Usage
To use the specific functions you can use:
import {
checkEmail,
checkDate,
checkPhoneNumber,
checkMACAddress,
CustomRegex,
} from "regex-validify";
And if you are working on a very big project it would be very hectic to import each function separately. Well, don't worry, in that case you can simply import the container which will contain all the methods, isn't that easy and cool ๐.
import Container from "regex-validify"
Functions
1. checkEmail
Validates if the input is a valid email address.
checkEmail(email: string): boolean
2. checkDate
Validates if the input date is in the correct format (e.g., DD/MM/YYYY).
checkDate(date: string): boolean
3. checkPhoneNumber
Validates if the input is a valid phone number.
checkPhoneNumber(phone: string): boolean
4. checkURL
Validates if the input is a valid URL.
checkURL(url: string): boolean
5. checkAlpha
Checks if the input string contains only letters.
checkOnlyLetters(input: string): boolean
6. checkPassword
Validates the strength of the password. By default the complexity parameter set to false, if you want a complex password validation then make the second arguement true.
checkPassword(password: string, complexity?: boolean): boolean
7. checkOnlyIntegers
Checks if the input string contains only integers.
checkOnlyIntegers(input: string): boolean
8. checkCreditCard
Validates credit card numbers.
checkCreditCard(cardNumber: string): boolean
9. checkUsername
Validates if the input is a valid username.
checkUsername(username: string): boolean
10. checkIPv4
Validates if the input is a valid IPv4 address.
checkIPv4(ip: string): boolean
11. checkHexColor
Validates if the input is a valid hex color code.
checkHexColor(color: string): boolean
12. checkMACAddress
Validates if the input is a valid MAC address.
checkMACAddress(mac: string): boolean
13. CustomRegex
Allows you to define your own regex for validation.
CustomRegex(pattern: (str: string) => boolean, input: string): boolean
Examples
Here are some examples demonstrating how to use the functions:
const testEmail = "[email protected]";
console.log(`Is the email '${testEmail}' valid?`, checkEmail(testEmail));
const testDate = "21/10/2024";
console.log(`Is the date '${testDate}' valid?`, checkDate(testDate));
const testPhone = "+1234567890";
console.log(`Is the phone number '${testPhone}' valid?`, checkPhoneNumber(testPhone));
const testURL = "https://example.com";
console.log(`Is the URL '${testURL}' valid?`, checkURL(testURL));
const testPassword = "P@ssw0rd123!";
console.log(`Is the password '${testPassword}' valid?`, checkPassword(testPassword, true));
const customPattern = (str: string) => /^[a-zA-Z]+$/.test(str);
console.log(`Does 'Hello' match custom pattern?`, CustomRegex(customPattern, "Hello"));
License
This project is licensed under the ISC License.