@babl.one/validate
v0.1.0
Published
Validation Functions
Downloads
12
Readme
babl.one :: plugins/validate
This class provides various static methods for validating different types of data such as phone numbers, emails, usernames, passwords, and more. It is designed to be used in applications where input validation is required.
Methods
phone(value: string | undefined): boolean
Validates if the given value is a valid phone number.
- Returns
trueif valid,falseotherwise.
email(value: string | undefined): boolean
Validates if the given value is a valid email address.
- Returns
trueif valid,falseotherwise.
username(value: string | undefined, moderateWords: string[], bannedWords: string[]): string | boolean
Validates if the given value is a valid username.
- Checks for:
- Minimum length of 4 characters.
- Only alphanumeric characters and underscores.
- The username does not contain any moderate or banned words.
- Returns a specific error code if invalid, or
trueif valid.
verification(value: string | undefined): boolean
Validates if the value is a 6-digit numeric verification code.
- Returns
trueif valid,falseotherwise.
password(value: string | undefined): boolean
Validates if the given value is a valid password (minimum length of 6 characters).
- Returns
trueif valid,falseotherwise.
gender(value: string | undefined, genders: Record<string, string>): boolean
Validates if the given value is a valid gender.
- Returns
trueif valid,falseotherwise.
dob(value: string | undefined): boolean
Validates if the given value is a valid date of birth and checks if the age is 18 or older.
- Returns
trueif valid,falseotherwise.
blank(value: string | undefined): boolean
Checks if the given value is not blank (i.e., not empty or only whitespace).
- Returns
trueif not blank,falseotherwise.
minLength(value: string | undefined, min: number): boolean
Checks if the given value has a minimum length.
- Returns
trueif valid,falseotherwise.
inArray(value: string | undefined, haystack: string[]): boolean
Checks if the given value exists in the provided array.
- Returns
trueif found,falseotherwise.
creditCardNumber(value: string | undefined): boolean
Validates if the given value is a valid credit card number using the Luhn algorithm.
- Returns
trueif valid,falseotherwise.
creditCardExpiration(month: number, year: number): boolean
Validates if the given credit card expiration month and year are valid.
- Returns
trueif valid,falseotherwise.
cvv(cardNumber: string, cvv: string): boolean
Validates if the given CVV is valid for the provided card number.
- Returns
trueif valid,falseotherwise.
amount(value: string | undefined): boolean
Validates if the given value is a valid currency amount (supports two decimal places).
- Returns
trueif valid,falseotherwise.
number(value: any): boolean
Checks if the given value is a valid number.
- Returns
trueif valid,falseotherwise.
Usage Example
import Validate from './validate';
// Validate phone number
const isValidPhone = Validate.phone('+1 (123) 456-7890');
console.log(isValidPhone); // true
// Validate email
const isValidEmail = Validate.email('[email protected]');
console.log(isValidEmail); // true
// Validate username
const isValidUsername = Validate.username('user_123', ['badword'], ['banned']);
console.log(isValidUsername); // true or error code
// Validate password
const isValidPassword = Validate.password('securepassword123');
console.log(isValidPassword); // trueLicense
MIT License.
