Overview
A formatted input is a text field that keeps its value in one shape as the user types or pastes. .nds-formatted goes on the .nds-form-container of a text field: the label, the messages and submit validation work as on every field. data-format on the same element sets the shape. A built-in kind: phone for a phone number, national-id for a national ID (a Saudi ID or iqama number, unless the page sets another country), iban for a bank account IBAN, plate for a Saudi vehicle plate, and vat for a Saudi VAT number. Or a shape you write, for a value such as a reference number (AA-000000), or the name of one a script added (card).
Pick another component when:
- the value has no fixed shape, such as a name or an email address: Text Fields
- the user types a one-time code: OTP Input
- the user picks a date or a time: Date Picker, Time Picker
Markup
Variants
Each Structure is one kind of value. Every one carries .nds-formatted and a data-format: a built-in kind, or a shape you write. The size, style, states and messages of a text field work on all seven: see Text Fields.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Phone (default) | — | — | A Saudi mobile number after a fixed +966. A hidden input in the prefix holds the code, and data-groups sets the groups |
| Structure | Phone with country (id: phone-country) | canon #field-phone-country |
— | A phone number after a country-code picker. The picker is a Dropmenu with data-select-name. Each country item’s data-value is its code, and it may carry its own data-groups |
| Structure | National ID (id: national-id) (demo: + required) | canon #field-national-id |
— | A national ID: by default a Saudi ID or iqama number, with digits only, 10 at most, and a check digit. Another country follows its own rule: see Site Country |
| Structure | IBAN (id: iban) (demo: + required) | canon #field-iban |
— | A bank account IBAN. The field shows it in groups of four, writes capitals, adds missing country letters (SA by default), and checks the country, the length and the check digits |
| Structure | VAT number (id: vat) (demo: + required) | canon #field-vat |
— | A Saudi VAT number: 15 digits, the first and the last 3 |
| Structure | Custom format (id: formatted) (demo: + required) (demo: + pattern) | canon #field-formatted |
— | A value in a shape you write in data-format: 0 takes a digit, A a capital letter, a a small letter, * a letter or a digit as typed, ح an Arabic letter. A value that leaves a slot empty fails. Add a pattern only for a rule the shape cannot hold |
| Structure | Saudi plate (id: plate) (demo: + required) | canon #field-plate |
— | A Saudi vehicle plate: letters, then digits. The field takes the 17 plate letters in Arabic or Latin, shows them in the field’s language, and submits the Latin letters, then the digits (ABJ1234) |
| Affix style | Subtle (default) | .nds-subtle |
.nds-prefix > .nds-btn |
The code or the country picker on the field’s own background |
| Affix style | Solid (hint: Light fill behind the code) | .nds-secondary |
.nds-prefix > .nds-btn |
The code or the country picker on a light fill, set apart from the number. Phone only |
| Letters | Auto (default) | — | [data-format="plate"] > .nds-form-control |
The first letter the user types picks the script: Arabic or Latin. An empty field follows the page’s lang. Plate only |
| Letters | Arabic (hint: أ ب ح ١٢٣٤, right to left) | [lang="ar"] |
[data-format="plate"] > .nds-form-control |
Arabic letters apart and Arabic digits, right to left, as the plate’s Arabic row: أ ب ح ١٢٣٤ |
| Letters | English (hint: ABJ 1234, left to right) | [lang="en"] |
[data-format="plate"] > .nds-form-control |
Latin letters, left to right: ABJ 1234 |
| Vehicle | Car (default) | — | [data-format="plate"] |
1 to 3 letters and 1 to 4 digits |
| Vehicle | Motorcycle | [data-vehicle="motorcycle"] |
[data-format="plate"] |
1 or 2 letters and 1 to 3 digits |
| Submit Arabic | Submit Arabic (hint: Sends أبح1234) | [data-submit="arabic"] |
[data-format="plate"] |
The submit sends Arabic letters, then the digits: أبح1234. For a server that stores the plate in Arabic |
| Required | Required (id: required) | [data-required] |
.nds-form-container |
A red asterisk before the label. An empty field blocks the submit |
| Pattern | Pattern (id: pattern) (hint: The value must start with AB) | [pattern="AB-[0-9]{6}"] |
#application-number |
A rule the shape cannot hold, here the letters AB. A value that breaks it fails with “Please use the correct format”. Write the pattern with the separators the field shows. Custom format only |
| Field states | Size, style, label, info, hint, disabled, readonly, loading | — | — | Shared by every text field. See Text Fields |
| Site country | Site country | — | — | data-country="AE" on <html> or any element around the field. Without it, the country is SA. Not a builder option: see Site Country |
Behavior
Phone Number
A phone field, its input type="tel", keeps only the digits. A number holds at most 15 digits, the country code included, or the input’s maxlength. A field without a country code sends the number as the user typed it, with its leading zero (0551234567). The whole row reads left to right, its country code first, as a number is written: +966 55 123 4567 on an Arabic page too.
Country Code
A phone’s code is the value of the hidden input in its .nds-prefix. With a fixed +966, write that input yourself, next to its label. A country picker needs none: Dropmenu writes it, from the data-value of the picked country item. The field then shows the local number. A submit and new FormData(form) send the whole number in E.164 form: the country code, then the digits, with no spaces (+966551234567). input.value keeps the local number, without the code.
A pasted full number, such as +966 55 123 4567 or 00966551234567, loses the code. A pasted number with the code but no +, such as 966551234567, loses the code when it is longer than the field holds (data-groups or maxlength). A zero at the start of the local number is a trunk prefix, so the field removes it. Numbers in Italy, San Marino and Vatican City (+39, +378, +379) keep it.
Country Picker
Phone with country puts a Dropmenu with data-select-name before the input. The picker writes the picked code to a hidden input of that name, so the code also submits as its own field. A pasted full number from another listed country picks that country. A new pick shows the number in the groups of that country.
Number Groups
data-groups on the field’s .nds-form-container shows the number in groups. Each 0 is one digit, and any other character goes between them: 00 000 0000 shows 55 123 4567. The field then takes no more digits than the format holds, and removes a maxlength shorter than the format. A number that leaves a slot empty fails with “Incomplete value”. With a country picker, the field reads data-groups from the picked country item instead. A country without one shows the number as typed.
National ID
national-id follows the ID rule of its country: see Site Country. The Saudi rule, the default, keeps digits only and holds 10 at most. The field fails when the value is not a Saudi national ID or iqama number: 10 digits, the first 1 or 2, and a correct check digit.
IBAN
iban shows the IBAN in groups of four and writes the letters as capitals. It takes two letters for the country, two check digits, then letters or digits. It holds 34 characters at most, the longest IBAN, and removes a shorter maxlength. An IBAN that starts with SA takes digits only after it, 24 characters in all. A value that starts with a digit, as some banking apps copy a Saudi IBAN, gets the letters of the site country in front, SA by default. The field fails when the value is not an IBAN: two letters for the country, two check digits, then 11 to 30 letters or digits, with correct check digits. The check takes an IBAN of any country, whatever the site country is.
VAT Number
vat takes a Saudi VAT number: 15 digits, the first and the last 3 (300000000000003). A number with fewer digits fails with “Incomplete value”, and one that does not start and end with 3 fails with “Invalid VAT number”. ZATCA does not publish the rule of its check digit, so the field cannot check it. Only ZATCA’s lookup says a number is registered.
Saudi Plate
plate takes a Saudi vehicle plate: letters, then digits. It takes only the 17 letters a plate uses, typed in Arabic or Latin: أ or A, ب or B, ح or J, and so on. It also takes ا for أ, ي for ى, and ه for هـ. A car has 1 to 3 letters and 1 to 4 digits. data-vehicle="motorcycle" on the .nds-form-container sets 1 or 2 letters and 1 to 3 digits. Letters always come before the digits, so a pasted 1234 ABJ shows ABJ 1234.
The first letter the user types picks the script: type أ and the field shows Arabic, type A and it shows Latin. An empty field follows the page’s lang. To fix one script, write lang on its .nds-form-control. With Arabic, the whole row flips, its actions and icons included, and the field shows the Arabic letters apart and Arabic digits, right to left, as the plate’s Arabic row: أ ب ح ١٢٣٤. With Latin, it shows ABJ 1234. Either way, the submit sends the Latin letters, then the digits: ABJ1234. data-submit="arabic" on the .nds-form-container sends Arabic letters instead: أبح1234. A value the page writes back in value shows the same in either script. A value with no letter or no digit fails with “Incomplete value”.
Leave out the placeholder: the field writes a sample plate that follows its letters and vehicle, ABJ 1234, AB 123, أ ب ح ١٢٣٤ or أ ب ١٢٣. A placeholder you write stays as written.
Site Country
data-country names the country that a national ID and an IBAN follow, as a two-letter code such as AE. Write it on <html> for the whole site, or on any element for the fields inside it: the nearest one wins. Without it, the country is SA. any takes an ID of any country, a passport too, with no check. Any other value that is not two letters logs one warning, and SA applies.
NDS ships the ID rule of Saudi Arabia only. Add the rule of another country with NDS.FormattedInput.addIdRule(), and every field of that country takes it at once. In a country with no rule, and with any, a national ID keeps letters and digits, writes the letters as capitals, and is not checked. An IBAN under any gets no letters in front.
A form that asks for the ID type first (national ID, iqama, passport) keeps one number field. Set its data-country when the type changes, and call NDS.Form.refresh(input) so the field takes the new rule at once:
Custom Format
.nds-formatted takes any value with a fixed shape. Write the shape in data-format on its .nds-form-container. 0 takes a digit, A a letter written as a capital, a a letter written small, * a letter or a digit as typed, and ح an Arabic letter. The field writes every other character for the user: AA-000000 shows AB-123456 when the user types ab123456, and AB- when the user types the dash too. For a Saudi plate, use data-format="plate". A key that does not fit the next slot is dropped, and the field stops at the last slot.
A value that leaves a slot empty fails with “Incomplete value”, at blur and at submit. An empty field passes unless it is required. For a rule the shape cannot hold, add a pattern to the input. It checks the value as the field shows it, separators included: [34]-[0-9]{6} takes only a first digit of 3 or 4 in a 0-000000 shape.
A delete can move letters onto digit slots, as when the user deletes AB from AB-123456. The field then keeps every character until the value fits its shape again. It never drops a character the user did not just type. An IBAN works the same way.
The field reads data-format at each key, so a script can change the shape at any time. Call NDS.Form.refresh(input) after the change to reshape the value the field already holds. A rule can also live in a script, with setCustomValidity() as on any text field. “Incomplete value” shows first while a slot is empty.
Named Formats
A shape that needs a check the slots cannot hold, such as the check digit of a card number, is a named format. Add it once in a script with NDS.FormattedInput.addFormat(name, rule), and write its name in data-format: data-format="card". The rule gives the slots in format, and check(value) returns true for a valid value. check gets the value the form submits: its letters and digits, with no separators. A full value that fails shows message, or “Invalid value”. A value with an empty slot shows “Incomplete value” first.
A name is small letters, digits and dashes, with at least one letter other than a, so it never reads as a shape. The built-in kinds’ names are taken. A field whose name is not added yet keeps its value as typed. When addFormat() runs, every field that names it takes the shape at once, so a script that loads after NDS still works.
Built-in Features
-
Auto-initialization
Every field starts when the page loads, and a field added later starts on its own. The first field on a page that had none needs
NDS.init.mount()on the new content. -
Shape While Typing
Each key and each paste takes the field's shape at once, and the cursor stays where the user typed. A separator the user types after the last character shows too. A delete next to a separator removes the character past it as well.
-
Arabic Digits
Arabic-Indic digits go in as Latin ones, typed or pasted, in every field.
-
Left-to-Right Values
Every formatted field reads left to right on every page, so its groups keep their order on an Arabic page. The value stays on the page side, as in the other fields.
-
Paste Cleanup
A paste loses the spaces, dashes and other characters the field does not take.
-
Checksum Validation
An IBAN, or a national ID whose country has a rule, fails the field through the browser's own check when its check digits are wrong. A field with a shape fails when a slot is left empty. The message shows at blur and at submit, in Arabic or English to match the page. An empty field passes unless it is required.
-
Clean Submit
A submit and
new FormData(form)send the value without its separators.input.valuekeeps them. -
Form Reset
A form reset puts each field back to the value the page shipped with, in its shape.
Best Practices
- Write
.nds-formattedand adata-formaton every field. A built-in kind without.nds-formattedis a plain text box:NDS.init.audit()reports it. - Use
type="tel"andinputmode="numeric"on a phone. Useinputmode="numeric"on a national ID, a VAT number or a custom format of digits only. Leaveinputmodeoff a field that takes letters, such as an IBAN or a plate: a phone keyboard would show digits only. - Set
autocomplete="tel-national"on a phone with a code, andautocomplete="off"on every other formatted field. - Give every phone field that shows a code its hidden code input, or a picker whose items’
data-valueis the code. Without a code, the submit sends the local number only. - Read the phone number from the form’s data, or keep the
valueofnds:phone:change.input.valueholds the local number with its spaces. - Write a phone’s
data-groupsonly for a country whose number length you know. Phone numbers differ in length between countries, and even inside one. - Keep
00 000 0000for a Saudi mobile only. A field that takes any Saudi number, a landline or an800number too, has nodata-groups: the groups hold 9 digits and drops the last digit of800 123 4567. - Add no
patternto a national ID, an IBAN, a plate or a VAT field. The field checks itself. - Give a Saudi national ID
maxlength="10". The browser then stops at 10 before the script loads. - On a site outside Saudi Arabia, set
data-countryon<html>, and add your country’s ID rule withaddIdRule(). - For a passport or a foreign ID, set
data-country="any"on the field, and leaveinputmodeandmaxlengthoff. - For a value with a known shape that no built-in kind covers, such as a reference number, write the shape in
data-format. The shape already fails a value that is too short. Add apatternand adata-error-messageonly for a rule the shape cannot hold, and write the pattern with the separators the field shows. - To fill a field from a script, call
NDS.FormattedInput.setValue(input, text). The field then takes its shape.
API
Classes
| Class | Element | Effect |
|---|---|---|
nds-formatted |
.nds-form-container |
Makes the field a formatted input. Its data-format sets the shape. Without one, it keeps the value as typed |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
input type="hidden" |
in the .nds-prefix of a phone field |
Its value is the code the submit puts before the number (+966). The field also drops it from a pasted full number. Give it a name to also send the code as its own field |
data-format |
the .nds-form-container of a .nds-formatted field |
The shape. A built-in kind: phone (keeps the digits; with a code, submits the whole number, +966551234567), national-id (follows the ID rule of its country, Saudi Arabia by default, and fails a wrong number), iban (groups of four, capitals, missing country letters added, fails a wrong IBAN), plate (a Saudi plate’s letters and digits, submitted as ABJ1234), or vat (a Saudi VAT number: 15 digits, the first and the last 3). Or the name of a format addFormat() added (card), or the shape of the value: 0 takes a digit, A a capital letter, a a small letter, * a letter or a digit as typed, ح an Arabic letter, and any other character goes between them (AA-000000, ححح 0000) |
data-select-name |
the .nds-dropmenu in a phone field’s .nds-form-control |
Makes the menu the field’s country picker. The picked code submits in a hidden input of this name. See Dropmenu |
data-select-value |
the same .nds-dropmenu |
The data-value of the country picked at page load. It wins over an item with data-state="selected" |
data-value |
a .nds-dropmenu-item in the country picker |
The code for that country (+971). Dropmenu writes the picked one to the picker’s hidden input |
data-groups |
the .nds-form-container of a phone field |
The groups the number shows in: 0 is one digit, and any other character goes between digits (00 000 0000) |
data-groups |
a .nds-dropmenu-item in the country picker |
The groups for that country, read while it is picked. A country without one shows the number as typed |
data-country |
<html>, or any element around a national ID or IBAN field |
The two-letter country the fields inside it follow (AE). The nearest one wins. Without one, SA. any takes an ID of any country, a passport too, with no check |
data-vehicle |
the .nds-form-container of a plate field |
motorcycle: 1 or 2 letters and 1 to 3 digits. Without it, a car: 1 to 3 letters and 1 to 4 digits |
lang |
the .nds-form-control of a plate field |
Fixes the script. ar flips the row, its actions and icons included, and shows the Arabic letters apart and Arabic digits, right to left (أ ب ح ١٢٣٤). Any other language shows the Latin letters (ABJ 1234). Without it, the first letter typed picks the script, and the field writes lang there itself. An empty field follows the page’s lang |
data-submit |
the .nds-form-container of a plate field |
arabic: the submit sends Arabic letters, then the digits (أبح1234). Without it, Latin letters (ABJ1234) |
placeholder |
the input of a plate field | Leave it out: the field writes a sample plate in its letters and vehicle (AB 123 for a motorcycle). One you write stays |
maxlength |
the input of a .nds-formatted field |
The most characters, for a field with no groups. A field with a shape or data-groups, and an IBAN field, remove a maxlength shorter than their groups. A plate field removes any maxlength |
Validation Messages
The script shows the message in Arabic or English to match the page. The other checks on these fields, such as required, are the ones every text field shows.
| Check | Message |
|---|---|
national-id |
Invalid national ID number |
iban |
Invalid IBAN |
vat |
Invalid VAT number |
A slot left empty, in a shape or a phone’s data-groups |
Incomplete value |
A named format whose check fails |
Its message, or Invalid value |
plate with no letter or no digit |
Incomplete value |
JavaScript
| Method | Effect |
|---|---|
NDS.FormattedInput.getValue(el) |
Returns the value the form submits: no group spaces, a phone in E.164, a plate in Latin. It returns '' when the field is not found. el is the .nds-formatted container, its input, its id or a selector |
NDS.FormattedInput.setValue(el, text) |
Writes the text as a user does, and the field formats it. Returns true, even when the text is invalid: the field then shows the usual invalid status. It returns false only when the field is not found |
NDS.FormattedInput.clear(el) |
Empties the field |
NDS.FormattedInput.isNationalId(value, country), isIban(value) |
Return true when the string passes the national-id or iban check. They take the clean value: Latin digits only, or capitals with no spaces. Remove an IBAN field’s spaces first: input.value.replace(/\s/g, ''). country is SA by default. A country with no rule passes |
NDS.FormattedInput.addFormat(name, rule) |
Adds a named format for data-format="name", or replaces one. rule is { format, check, message }: format the slots, check(value) returns true for a valid value (the value the form submits), message shows when it fails. check and message are optional. See Named Formats |
NDS.FormattedInput.addIdRule(country, rule) |
Adds the national ID rule of a country ('AE'), or replaces one. rule is { check, digits, length }: check(value) returns true for a valid clean value, digits: true keeps digits only (else capitals and digits), and length is the most characters. Fields already on the page take it at once |
| Event | Fired on | Detail |
|---|---|---|
nds:phone:change |
the input of a phone field, and it bubbles | { value, countryCode, nationalNumber }, when the number the field submits changes: by typing, a paste, a country pick, a reset or NDS.Form.refresh(). value is that number (+966551234567). countryCode and nationalNumber are null for a phone without a code. A field fires none at page load |
The full API is in the banner of _js/nds-formatted-input.js.