1.0.1 • Published 1 year ago
@devspeed/validatorjs v1.0.1
@devspeed/validatorjs
A JavaScript library for validating input strings based on various rules
Installation
Install using npm:
npm install @devspeed/validatorjs
Usage
To use the library, simply import it into your project:
import Validator from '@devspeed/validatorjs';
const validator = new Validator('example@gmail.com', {
validateEmpty: false,
validateEmail: true,
maxLength: 50,
});
const validationResult = validator.getValidation();
console.log(validationResult.isValid); // true
Methods
Method | Description | |
---|---|---|
isEmpty() | Checks if the input string is empty. | |
maxLength(number) | Checks if the input string exceeds the specified maximum length. | |
minLength(number) | Checks if the input string is shorter than the specified minimum length. | |
isEmail() | Checks if the input string is a valid email address. | |
isDate() | Checks if the input string is a valid date in the format DD/MM/YYYY. | |
requireUppercase() | Checks if the input string contains at least one uppercase letter. | |
requireLowercase() | Checks if the input string contains at least one lowercase letter. | |
containSpecialChar(specialChar) | Checks if the input string contains the specified special character. | |
getValidation() | Returns an object containing a boolean value indicating whether the input string is valid, as | well as an array of validation errors if applicable. |
Note: All methods return the Validator instance to allow chaining.
Options
The Validator constructor takes an optional options object that can contain the following properties:
Option | Description |
---|---|
validateEmpty | Whether to validate that the input string is not empty (default: true). |
validateEmail | Whether to validate that the input string is a valid email address (default: false). |
validateDate | Whether to validate that the input string is a valid date in the format DD/MM/YYYY (default: false). |
maxLength | The maximum allowed length for the input string. |
minLength | The minimum required length for the input string. |
requireUppercase | Whether the input string must contain at least one uppercase letter (default: false). |
requireLowercase | Whether the input string must contain at least one lowercase letter (default: false). |
License
This library is licensed under the MIT License.