Repository navigation
π¦ Validify
Update 3.0.0 Now Supports Typescript
A lightweight and powerful JavaScript validation library for usernames, passwords, emails, numbers, dates, URLs, phone numbers, and more β now with standardized error codes for cleaner error handling.
π Installation
Install the package using npm:
npm install input-validifyπ₯ Import & Usage
import Validify from "input-validify";
const vld = new Validify();π€ Username Validation
validateText(text, special = false, min = 2, max = 20, allowedSymbols = [])
Validates a text input such as a username by restricting special characters and setting length limits.
Parameters:
text: string β The input text to validate.special: boolean β Allow special characters (true) or not (false).min: number β Minimum character length.max: number β Maximum character length.allowedSymbols: string[] β Custom allowed special characters (optional).
Example:
vld.validateText("User_123", true, 2, 10, ['_', '-', '@']);π Password Validation
validatePassword(password, min = 6, max = 20, complexity = {})
Validates passwords for minimum/maximum length and character complexity.
Parameters:
password: string β The password to validate.min: number β Minimum character length.max: number β Maximum character length.complexity: object β Complexity rules:requireUppercase: booleanrequireLowercase: booleanrequireNumber: booleanrequireSpecialChar: boolean
Example:
vld.validatePassword("StrongPass123!", 8, 20, {
requireUppercase: true,
requireLowercase: true,
requireNumber: true,
requireSpecialChar: true,
});π§ Email Validation
validateEmail(email, regex?)
Validates email format using default or custom regular expression.
Parameters:
email: string β Email address to validate.regex: RegExp β Optional custom regex.
Example:
vld.validateEmail("example@email.com");
// With custom regex
vld.validateEmail("user@gmail.com", /^[a-z0-9._%+-]+@gmail\.com$/i);π’ Number Validation
validateNumber(number, min = 4, max = 20)
Validates number or numeric string length.
Parameters:
number: number|string β The number to validate.min: number β Minimum length.max: number β Maximum length.
Example:
vld.validateNumber(123456, 4, 10);π Date Validation
validateDate(date, format)
Validates date string based on format.
Supported Formats:
YYYY-MM-DDDD/MM/YYYYMM/DD/YYYYYYYY/MM/DDYYYY-MM-DD HH:mm:ssDD/MM/YYYY HH:mm:ssMM/DD/YYYY HH:mm:ssYYYY/MM/DD HH:mm:ssYYYY-MM-DDTHH:mm:ssYYYY-MM-DDTHH:mm:ss.SSS
Parameters:
date: string β The date string to validate.format: string β The expected format.
Example:
vld.validateDate("2025-03-15", "YYYY-MM-DD");
vld.validateDate("15/03/2025 10:30:00", "DD/MM/YYYY HH:mm:ss");π URL Validation
validateUrl(url)
Validates if the string is a valid URL.
Parameters:
url: string β The URL to validate.
Example:
vld.validateUrl("https://example.com");π Regex Validation
validateWithRegex(value, regex)
Custom validation using regular expressions.
Parameters:
value: string β The value to validate.regex: RegExp β The regular expression.
Example:
vld.validateWithRegex("abc123", /^[a-z0-9]+$/i);βοΈ Phone Number Validation
validatePhone(phoneNumber)
Validates phone numbers in various formats.
Parameters:
phoneNumber: string β The phone number to validate.
Example:
vld.validatePhone("+1 (123) 456-7890");
vld.validatePhone("123-456-7890");β Error Handling
All methods throw Error objects with error codes instead of plain text messages, so you can handle errors more programmatically:
Example:
try {
vld.validateText("");
} catch (err) {
console.log(err.message); // Example: "ERR_TEXT_EMPTY"
}π License
MIT License
π¨βπ» Author
Ali Abdullah
GitHub
Full Changelog: https://github.com/AliAbdullah0/npm-package-validify/commits/3.0.0