Skip to content
Focsd homeFocsd homeFocsd
Forms

Phone field

A phone number with a searchable country picker; the value is international (E.164).

  • Tag: focsd-phone-field
  • Pattern: Group: country combobox + tel input, E.164 value
  • Category: Forms

Example

HTML
<div class="stack">
<focsd-phone-field
label="Mobile"
default-country="GB"
preferred="GB,US,IE"></focsd-phone-field>
</div>

Usage

Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.

<script type="module">
import '@focsd-dev/components/focsd-phone-field.js';
</script>
<focsd-phone-field label="Mobile" default-country="GB" preferred="GB,US,IE"></focsd-phone-field>

API

Attributes

AttributePropertyTypeDefault
namenamestring—
disableddisabledbooleanfalse
requiredrequiredbooleanfalse
readonlyreadOnlybooleanfalse
aria-labelhostAriaLabelstring | nullnull
aria-descriptionhostAriaDescriptionstring | nullnull
aria-labelledbyhostAriaLabelledBystring | nullnull
aria-describedbyhostAriaDescribedBystring | nullnull
label-positionlabelPosition'top' | 'start' | 'end' | undefined—
hinthintstring—
errorerror——
warningwarningstring | undefined—
successsuccessstring | undefined—
error-messageerrorMessagestring | undefined—
warning-messagewarningMessagestring | undefined—
warning-patternwarningPatternstring | undefined—
labellabelstring—
valuevaluestring—
countrycountrystring—
default-countrydefaultCountrystring—
countriescountriesstring—
preferredpreferredstring—

Methods

MethodDescription
describedByIds()IDs to add to the inner control's aria-describedby: the hint, then the shown message.
checkValidity()True when the value is valid; otherwise fires invalid and returns false, like native.
reportValidity()Like checkValidity(), and also shows the browser's validation message when invalid.
setCustomValidity(message)Set (non-empty) or clear (empty string) a custom validation error, like native controls.
setValidityState(flags, message, anchor)For subclasses: set the validity flags and message. anchor is the element the browser points its message at.
validate()Show (or clear) the error from the control's own checks: required, type, pattern, min, max and the like. Uses error-message, else the browser's localized message. An error the author set (a server error, say) is left as it is.
clearValidation()Clear the error and warning that validate() and warning-pattern showed (a form reset does).
emitValue(commit)For subclasses: fire the value events, as a native control does on a user edit: input (bubbles, composed), then with commit also change (bubbles, stays in its tree).
idrefText(ids)For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces.
checkPhone()Check the number: missing (with required), rejected by validator, or not a valid number for the country. Returns null when it passes.
requestValidation()Re-run validation, for example after changing validator.

Events

EventDescription
inputFired when the number or the country changes.
changeFired when the person commits a change.

CSS parts

Style the inside of the component with ::part(), for example focsd-phone-field::part(label).

PartDescription
labelThe label.
layoutThe label and the body (see label position).
bodyThe field with its example, hint and message.
hintThe author hint.
messageThe shown error, warning or success message.
message-iconThe icon of the message.
fieldThe group holding both inputs.
flagThe flag of the chosen country.
countryThe country combobox.
popupThe country list popup.
listboxThe country list.
optionOne country.
controlThe number input.
exampleThe example number.
statusThe live status (visually hidden).
announcerThe visually hidden status region that announces a new message.

Accessibility

  • One role="group" named by the label holds a "Country" combobox and a type="tel" input.
  • The tel input is described by your hint, then an example number for the chosen country, then the shown message, and uses autocomplete="tel-national". Style the example with ::part(example); ::part(hint) is your hint, as on every control.
  • Typing or pasting a number with + or 00 switches the country, announced in a polite status.
  • Invalid numbers report "Enter a valid United Kingdom phone number"; pass validator for stricter checks.
  • The value and the form entry are E.164 (+442071838750), so servers never parse local formats.

Keyboard

KeyAction
Type in CountryFilter by name, code or dial code (+353)
Down / UpMove through countries
Home / End (open list)First / last country
EnterChoose the country
EscapeClose the list and keep the country