validators()
validators checks one value against a named rule and returns whether it's valid.
validators(value, name, options)
| Parameter | Description |
|---|---|
value | The value to check: text, a number, a Date, or null |
name | A base rule, a preset, or null to check only the options |
options | Optional. The options that add checks or change messages |
Returns
validators returns one of these objects:
{ valid: true }
{ valid: false, code, message }
The code names the check that failed, and the message is the text to show.
When a preset's own check fails, the code is the preset's name.
When a check that you added fails, the code is that option's name.
For every code and its default message, see Error codes and messages.
Throws
validators throws an error for a wrong call:
- An unknown rule name or option
- A
regexthat uses\d,\w, or\s, or that doesn't compile - An unknown
timezone - A date bound that isn't a date,
"today","now", or{ addDays: n }
Base rules
A base rule is a general kind of check. You narrow a base rule with options. The package has seven base rules:
| Rule | Accepts |
|---|---|
alphaNumeric | Letters and digits |
lettersOnly | Letters and spaces |
numbersOnly | Digits only, checked as characters: "007" passes, and "12.5" and "-3" fail |
number | A number value, such as -18.5, 12, or 0.75 |
email | An address in the form name@domain.tld |
password | Eight or more characters, with an upper-case letter, a lower-case letter, a digit, and one of @$!%*?& |
date | 2026-10-07, 2026-10-07 14:30:00 (UTC), ISO 8601 text with T, Z, or +05:30, or a Date; impossible dates such as 2026-02-30 fail |
Presets
A preset is a base rule, a pattern, or both, with options already set, saved under one name.
Your options override the preset's options, so validators(v, "mobile", { length: 12 }) accepts 12 digits.
The package has 16 presets:
| Preset | Accepts |
|---|---|
integer | A number with no decimals |
positiveNumber | A number greater than 0 |
amount | A number of 0 or more, with up to two decimals |
weight | A number of 0 or more, with up to three decimals |
percentage | A number from 0 to 100, with up to two decimals |
decimalUpTo2 | A number of 0 or more, with up to two decimals |
decimalUpTo6 | A number of 0 or more, with up to six decimals |
mobile | A 10-digit mobile number |
pincode | A six-digit Postal Index Number (PIN) code |
vehicleNumber | A vehicle registration number, such as KA01AB1234, DL1C1234, or the Bharat (BH) series 22BH1234AA |
gstin | A Goods and Services Tax Identification Number (GSTIN), such as 27ABCDE1234F1Z5 |
pan | A Permanent Account Number (PAN), such as ABCDE1234F |
ifsc | An Indian Financial System Code (IFSC), such as HDFC0001234 |
iec | An Importer-Exporter Code (IEC) of 10 letters or digits |
pastDate | A date that isn't after today |
futureDate | A date that isn't before today |
The ID presets pan, gstin, ifsc, iec, and vehicleNumber expect upper case.
Pair them with the formatter of the same name.
For details, see Format and validate a form field.
Options
validators takes these options:
| Option | Meaning |
|---|---|
required | When true, a blank value fails |
label | The field name used in the required message |
message | Replaces the default message of every check except required |
length | Exactly this many characters |
minLength | At least this many characters |
maxLength | At most this many characters |
minValue | The smallest number allowed, checked only when the value is a number |
maxValue | The largest number allowed, checked only when the value is a number |
decimalPlaces | At most this many digits after the decimal point; 0 allows none |
regex | A pattern the whole value must match, such as "^[A-Z]{4}[0-9]{7}$"; see Patterns |
minDate | The earliest date allowed, inclusive |
maxDate | The latest date allowed, inclusive |
after | A date the value must be after, exclusive |
before | A date the value must be before, exclusive |
timezone | The zone for "today" and for dates in messages: "Asia/Kolkata" (default), "UTC", or an offset such as "+05:30" |
now | The current time to compare against, instead of the clock; use it in tests |
Order of checks
The checks run in this order, and the first failure is returned:
required- The rule
regexdecimalPlacesminValueandmaxValuelength,minLength, andmaxLength- The date bounds
Blank values
A blank value passes every check except required, so optional fields need nothing special.
A blank value is null, undefined, empty or whitespace-only text, or [].
Date bounds
The minDate, maxDate, after, and before options each accept these values:
- A date, such as
"2026-10-07","2026-10-07 14:30:00", ISO 8601 text, or aDate "today", the calendar day intimezone"now", the current instant{ addDays: n }, today plusndays, where a negativencounts back- Another field's value, such as
validators(bidEnd, "date", { after: bidStart })
When either side is a plain date, the package compares calendar days in timezone.
When both sides carry a time, it compares instants.
Patterns
The regex option takes text, not a JavaScript /regex/, so the same rule can sit in a JSON config and be sent to a Python service.
The value must match the whole pattern.
validators("ABCD1234567", null, { regex: "^[A-Z]{4}[0-9]{7}$", message: "Enter a valid container number" })
Don't use \d, \w, or \s in a pattern; the call throws an error if you do.
Python reads them differently: its \d matches every Unicode digit.
Write [0-9], [A-Za-z0-9_], and [ \t] instead.
Use only pattern features that both languages share: character classes, groups, {n,m}, |, and lookaheads.
Examples
The following examples show common calls:
validators("560001", "pincode") // { valid: true }
validators("56001", "pincode") // { valid: false, code: "pincode", message: "Enter a valid 6-digit PIN code" }
validators("", "pincode", { required: true, label: "Delivery PIN" }) // message: "Delivery PIN is required"
validators("abcde1234f", "pan") // message: "Enter a valid PAN (e.g. ABCDE1234F)"
validators("0.5", "number", { decimalPlaces: 3, minValue: 1 }) // message: "Minimum value is 1"
validators("0", "integer", { minValue: 1 }) // code: "minValue"
validators("2026-10-07", "date", { minDate: "today", maxDate: { addDays: 30 } })
validators("Al", null, { minLength: 3 }) // { valid: false, code: "minLength", message: "Minimum 3 characters required" }