Jevalide is a user-friendly JavaScript library for data validation.
npm install jevalideTired of writing complex validation logic? Jevalide is your solution for clean, maintainable form validation that just works.
Write your validation rules once and use them everywhere. Jevalide is a lightweight validation library that works seamlessly in both browser and Node.js environments. Whether you're building a complex web application or a simple Node.js service, Jevalide has got you covered.
No complexity - just simple, powerful validation when you need it.
Using npm:
``bash`
npm install jevalide
Jevalide simplifies validation with minimal setup. Here's how it works:
The Jevalide.validate method is perfect for quickly validating data without additional configuration:
`javascript
// Import Jevalide
import { Jevalide } from 'jevalide';
const data = {
email: 'test@example.com',
password: '12345'
};
const rules = {
email: ['required', 'email'],
password: ['required', 'minlength:8']
};
const validator = Jevalide.validate(data, rules);
if (validator.isValid()) {
console.log('Validation passed!');
} else {
console.log(validator.getErrors());
// Output: { password: 'Password must be at least 8 characters' }
}
`
The Jevalide.init method allows you to set up global configurations, such as custom rules, messages, and locales:
`javascript`
// Initialize with custom options
const validator = Jevalide.init({
rules: {
customRule: (value) => ({
passes: /^[a-zA-Z]+$/.test(value),
value
})
},
messages: {
required: 'This field is required',
email: 'Please enter a valid email',
customRule: 'Only letters are allowed'
},
local: 'en'
});
Effortlessly validate entire forms:
`javascript
const form = validator.form({
email: ['required', 'email'],
password: ['required', 'minlength:8']
}, {
email: 'user@example.com',
password: '12345'
});
if (form.passes()) {
console.log('All good!');
} else {
console.log(form.getErrors());
// Output: { password: 'Password must be at least 8 characters' }
}
`
Handle single input validation:
`javascript
const emailValidator = validator.input({
name: 'email',
rules: ['required', 'email']
});
emailValidator.setValue('invalid-email');
console.log(emailValidator.getError());
// Output: 'Please enter a valid email'
`
Add your own validation logic:
`javascript`
validator.rule('username', (value) => ({
passes: /^[a-zA-Z0-9_]+$/.test(value),
value
}), 'Username can only contain letters, numbers, and underscores');
Customize messages for different locales:
`javascript
validator.translate('fr', {
required: 'Ce champ est requis',
email: 'Veuillez entrer une adresse email valide'
});
validator.setLocale('fr');
`
For CommonJS environments:
`javascript`
const { Jevalide } = require('jevalide');
For browser environments, simply include Jevalide in your HTML:
`html`requiredSome Built-In Rules
| Name | Description |
| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| | Ensures the field is not empty. Works with strings, arrays, objects, numbers, booleans, etc. required
Example: |nullable
| | Always passes; effectively makes a field optional. nullable
Example: |in:x,y
| | Checks if the input value is among the comma-separated list (x, y, etc.). required\|in:admin,user,guest
Example: |size:x
| | If it’s a file, checks file size. Otherwise checks the exact length for strings. required\|size:5
Example: |boolean
| | Checks if the input is a boolean or a “boolean-like” string ("true","false","yes","no","1","0"). required\|boolean
Example: |between:x,y
| | General “range” check for numbers, strings, dates, or file size. Ensures the value is between x and y. required\|between:18,99
Example: |regex:pattern
| | Validates that the input matches the given regular expression. regex:^[0-9]{3}-[0-9]{4}$
Example: |only:string
| / only:digit | Restricts the input to either letters only (for "string") or strictly digits (for "digit"). only:string
Example: or only:digit |digit:x
| | Checks if the input is purely digits and exactly x digits long. digit:5
Example: |maxDigit:x
| | Checks if the input is purely digits and at most x digits long. maxDigit:10
Example: |minDigit:x
| | Checks if the input is purely digits and at least x digits long. minDigit:3
Example: |equal:value
| | Ensures the input is strictly equal (===) to the given value. equal:secret
Example: |same:fieldValue
| | Ensures the input loosely equals (==) another field’s value (useful for confirmations). same:password
Example: |object
| | Checks if the input is a valid object (not null or an array). Optionally enforce required keys. required\|object:name,age
Example: |json
| | Checks if the input is valid JSON. Optionally ensure required keys or indexes. json:key1,key2
Example: |array
| | Checks if the input is an array; optionally validate certain indexes. array:0,1
Example: |email
| | Validates if the input is a valid email address. required\|email
Example: |minlength:x
| | Ensures the string length is at least x. required\|minlength:8
Example: |maxlength:x
| | Ensures the string length does not exceed x. required\|maxlength:10
Example: |string
| | Checks if the value is a string. required\|string
Example: |url
| | Checks if the string is a valid URL. required\|url
Example: |startWithUpper
| | Ensures the string starts with an uppercase letter. startWithUpper
Example: |startWithLower
| | Ensures the string starts with a lowercase letter. startWithLower
Example: |startWith:x,y
| | Checks if the string starts with any of the given prefixes (x, y, etc.). startWith:pre1,pre2
Example: |endWith:x,y
| | Checks if the string ends with any of the given suffixes (x, y, etc.). endWith:suf1,suf2
Example: |contains:x,y
| | Checks if the string contains all of the listed substrings (x, y, etc.). contains:foo,bar
Example: |length:x
| | Validates that the string length is exactly x. length:9
Example: |password
| | Basic password complexity: length ≥ 8, uppercase, lowercase, number, special char. required\|password
Example: |startWithString
| | Ensures the input does not start with a digit. startWithString
Example: |endWithString
| | Ensures the input does not end with a digit. endWithString
Example: |hasLetter
| | Ensures the string contains at least one letter. hasLetter
Example: |excludes:x,y
| | Ensures none of the characters/strings listed (x, y) appear in the input. excludes:@,/,#
Example: |upper
| | Ensures the string is entirely uppercase. upper
Example: |lower
| | Ensures the string is entirely lowercase. lower
Example: |stringBetween:min,max
| | Ensures the string length is between min and max. stringBetween:2,5
Example: |date
| | Checks if the input is a valid date using Day.js. Returns true if valid, false otherwise. required\|date
Example: |before:x
| | Ensures the input date is before the date x. You can use "now" for the current date/time. required\|before:2020-01-01
Example: , required\|before:now |after:x
| | Ensures the input date is after the date x. You can use "now" for the current date/time. required\|after:2020-01-01
Example: , required\|after:now |dateBetween:x,y
| | Checks if the input date lies between the two dates x and y. You can use "now" in place of either bound. required\|dateBetween:2020-01-01,now
Example: |time
| | Checks if the input is a valid 24-hour time string (e.g., "HH:mm:ss"). If missing seconds, it automatically appends ":00" until the format is complete. required\|time
Example: |min:x
| | Checks the input value or character length against the minimum x. For numbers, ensures value >= x. For files, checks file size. required\|min:2
Example: |max:x
| | Checks the input value or character length against the maximum x. For numbers, ensures value <= x. For files, checks file size. required\|max:20
Example: |integer
| / int | Ensures the input is an integer. required\|integer
Example: , required\|int |number
| / numeric | Validates that the input can be parsed as a numeric value (not an object/boolean). required\|number
Example: |modulo:x
| / mod:x | Checks if the number is divisible by x. required\|modulo:2
Example: , required\|mod:2 |lessThan:x
| / lthan:x | Ensures the input value is strictly less than x. required\|lessThan:10
Example: , required\|lthan:10 |greaterThan:x
| / gthan:x | Ensures the input value is strictly greater than x. required\|greaterThan:5
Example: , required\|gthan:5 |numberBetween:x,y
| | Checks if the numeric input lies between x and y (inclusive). required\|numberBetween:1,10
Example: |phone
| | Validates if the input is a phone number (for example, using a custom phone rule). required\|phone
Example: |file
| | Checks if the input is a File, Blob, FileList, or an array of such items. required\|file
Example: |maxFileSize:x
| | Ensures the file size is at most x (e.g. 1MB). required\|maxFileSize:2MB
Example: |minFileSize:x
| | Ensures the file size is at least x (e.g. 1MB). required\|minFileSize:500KB
Example: |fileBetween:x,y
| | Checks that the file size is between x and y. required\|fileBetween:1MB,5MB
Example: |mimes:x,y
| | Validates the file’s MIME type/extension. Supports wildcards (.jpg), type groups (image/), or specific extensions. required\|mimes:.pdf,image/` |
Example:
With Jevalide, validation is no longer a hassle. Customize once, reuse everywhere, and keep your validation logic clean and maintainable.