JavaScript API
On pages where the phone field loads, the plugin adds window.NahnuSPF. Use it to add the flag dropdown to your own inputs, read numbers and check forms.
The script loads automatically on pages with the shortcodes, the checkout and account pages, and pages with a supported form. On other pages, call nahnu_spf_enqueue_assets() in PHP, or add your inputs with the nahnu_spf_selectors filter.
Wait for the API#
document.addEventListener('nahnu-spf:api-ready', (event) => {
const api = event.detail; // same as window.NahnuSPF
});
Methods#
| Method | What it does |
|---|---|
NahnuSPF.attach(input, options) | Adds the flag dropdown to an input (an element or a CSS selector) and returns the field. Returns the existing field if it already has one. |
NahnuSPF.get(input) | The field for an input that already has the dropdown, including the plugin’s own checkout fields. |
NahnuSPF.getAll(form) | All phone fields in a form or container. |
NahnuSPF.scan(container) | Sets up fields added to the page later. New fields are also found automatically. |
NahnuSPF.validateAll(form) | Checks every phone field in the form and shows the messages. Returns true when all are fine. |
NahnuSPF.prepare(form) | Puts the full international numbers in place, for forms sent by script. |
NahnuSPF.detectCountry() | A promise with the visitor’s country and state. |
Options for attach()#
| Option | What it does |
|---|---|
country | Two-letter starting country |
detect | Use location detection when no country or number is set |
validate, validateOnBlur, validateOnSubmit | Number checking, when the field loses focus and when the form is sent |
submitFullNumber | Send the full international number (default on) |
hiddenInput | Keep the full number in a hidden input with the field’s name, for forms sent by script |
onlyCountries, excludeCountries, preferredCountries | Arrays of country codes, instead of the site settings |
errorClass, inputErrorClass, errorScope, errorReplace, scopeErrorClass | Message styling, to match a form plugin |
intlTelInput | Any extra intl-tel-input options |
Field methods#
getNumber(), getReadable(), getCountry(), getCountryName(), isValid(), getNumberType(), getError(), validate(), showError(message), clearError(), prepare(), setNumber(number), setCountry(iso2), destroy(), and a ready promise.
Events#
Fired on the input. They bubble, so you can listen on the form or document.
| Event | When | Detail |
|---|---|---|
nahnu-spf:ready | A field is set up | The field |
nahnu-spf:change | The number or country changes | number, country, valid, field |
nahnu-spf:invalid | A form is stopped by a wrong number | field, message |
nahnu-spf:api-ready | The API is loaded (fired on document) | window.NahnuSPF |
Example#
const phone = NahnuSPF.attach('#my_phone', { country: 'gb' });
phone.ready.then(() => console.log(phone.getNumber(), phone.isValid()));
document.querySelector('#my_form').addEventListener('submit', (e) => {
if (!NahnuSPF.validateAll(e.target)) {
e.preventDefault();
}
});
Forms sent the normal way are handled automatically. Wrong numbers stop the form, and the full number is put in place before the form’s own submit handlers run.