Format and validate a form field
Clean a field's value while the user types, and then check it, using the same name for both steps.
Before you begin
- Install the package. For details, see Get started with Common Utilities.
- Find the field type for your field in Field types.
Clean and check a field
-
While the user types, pass the value through
formatterswith the field type:const shown = formatters(raw, "pan"); // "abcde 1234f" becomes "ABCDE1234F" -
When the field loses focus or the form is submitted, check the value with
validatorsand the same name:const result = validators(shown, "pan", { required: true, label: "PAN" }); -
If
result.validisfalse, showresult.messagenext to the field.
The field now accepts only the characters that the type allows, and shows one clear message when the value is wrong.
On the server, call validators with the same name and options to get the same answer.
Add limits to a field
Options add checks on top of the field type. For example, to accept an amount of at least 100:
validators(value, "amount", { minValue: 100 })
When an option that you added fails, the result's code is that option, here minValue.
When the field type's own check fails, the code is the field type's name.
For every option, see validators().
Field types
These names work with both formatters and validators:
| Field type | While typing, formatters | On check, validators accepts |
|---|---|---|
amount | Keeps digits and one decimal point, with two decimal places | A number of 0 or more, with up to two decimals |
weight | Keeps digits and one decimal point, with three decimal places | A number of 0 or more, with up to three decimals |
percentage | Keeps digits and one decimal point, with two decimal places and at most six characters | A number from 0 to 100, with up to two decimals |
decimalUpTo2 | Keeps digits and one decimal point, with two decimal places | A number of 0 or more, with up to two decimals |
decimalUpTo6 | Keeps digits and one decimal point, with six decimal places | A number of 0 or more, with up to six decimals |
mobile | Keeps digits, at most 10 | A 10-digit mobile number |
pincode | Keeps digits, at most six | A six-digit Postal Index Number (PIN) code |
email | Trims and converts to lower case | An address in the form name@domain.tld |
pan | Converts to upper case, keeps letters and digits, at most 10 | A Permanent Account Number (PAN), such as ABCDE1234F |
gstin | Converts to upper case, keeps letters and digits, at most 15 | A Goods and Services Tax Identification Number (GSTIN), such as 27ABCDE1234F1Z5 |
ifsc | Converts to upper case, keeps letters and digits, at most 11 | An Indian Financial System Code (IFSC), such as HDFC0001234 |
iec | Converts to upper case, keeps letters and digits, at most 10 | An Importer-Exporter Code (IEC) of 10 letters or digits |
vehicleNumber | Converts to upper case, keeps letters and digits, at most 10 | A vehicle registration number, such as KA01AB1234 or 22BH1234AA |
Some names exist on one side only:
- Only
validators:integer,positiveNumber,pastDate,futureDate, and the base rules such asnumberanddate - Only
formatters:decimalUpTo1,decimalUpTo3,decimalUpTo4,decimalUpTo5, and the base formatters such astrimandtitleCase
For a field without a shared name, pick a formatter and a validator separately, for example formatters(v, "numbersOnly") with validators(v, "integer").
For the message each check shows, see Error codes and messages.