Skip to main content

validators()

validators checks one value against a named rule and returns whether it's valid.

validators(value, name, options)
ParameterDescription
valueThe value to check: text, a number, a Date, or null
nameA base rule, a preset, or null to check only the options
optionsOptional. 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 regex that 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:

RuleAccepts
alphaNumericLetters and digits
lettersOnlyLetters and spaces
numbersOnlyDigits only, checked as characters: "007" passes, and "12.5" and "-3" fail
numberA number value, such as -18.5, 12, or 0.75
emailAn address in the form name@domain.tld
passwordEight or more characters, with an upper-case letter, a lower-case letter, a digit, and one of @$!%*?&
date2026-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:

PresetAccepts
integerA number with no decimals
positiveNumberA number greater than 0
amountA number of 0 or more, with up to two decimals
weightA number of 0 or more, with up to three decimals
percentageA number from 0 to 100, with up to two decimals
decimalUpTo2A number of 0 or more, with up to two decimals
decimalUpTo6A number of 0 or more, with up to six decimals
mobileA 10-digit mobile number
pincodeA six-digit Postal Index Number (PIN) code
vehicleNumberA vehicle registration number, such as KA01AB1234, DL1C1234, or the Bharat (BH) series 22BH1234AA
gstinA Goods and Services Tax Identification Number (GSTIN), such as 27ABCDE1234F1Z5
panA Permanent Account Number (PAN), such as ABCDE1234F
ifscAn Indian Financial System Code (IFSC), such as HDFC0001234
iecAn Importer-Exporter Code (IEC) of 10 letters or digits
pastDateA date that isn't after today
futureDateA 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:

OptionMeaning
requiredWhen true, a blank value fails
labelThe field name used in the required message
messageReplaces the default message of every check except required
lengthExactly this many characters
minLengthAt least this many characters
maxLengthAt most this many characters
minValueThe smallest number allowed, checked only when the value is a number
maxValueThe largest number allowed, checked only when the value is a number
decimalPlacesAt most this many digits after the decimal point; 0 allows none
regexA pattern the whole value must match, such as "^[A-Z]{4}[0-9]{7}$"; see Patterns
minDateThe earliest date allowed, inclusive
maxDateThe latest date allowed, inclusive
afterA date the value must be after, exclusive
beforeA date the value must be before, exclusive
timezoneThe zone for "today" and for dates in messages: "Asia/Kolkata" (default), "UTC", or an offset such as "+05:30"
nowThe 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:

  1. required
  2. The rule
  3. regex
  4. decimalPlaces
  5. minValue and maxValue
  6. length, minLength, and maxLength
  7. 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 a Date
  • "today", the calendar day in timezone
  • "now", the current instant
  • { addDays: n }, today plus n days, where a negative n counts 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" })
important

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" }