• Atom
  • CORE COMPONENT
  • Taxonomic name: A-TEXTBOX-FORMAT
  • Added on: v4.0.0 (20/07/18)
  • Updated on: v2023.01 (26/04/23)

Textbox format

Provides automatic formatting of textboxes to aid user input by adjusting single fields, for example memorable dates (such as date of birth), pin numbers and sort codes.


Design and usage

It is still possible (and permissable) to use standard textbox groups with individual inputs - see the textbox group page for further details. However focus must not be transferred automatically between the separate fields to maintain the accessibility and usability of the form.

Structure

  • Uses a standard Framework form text field with a text keyboard
  • If used for inputting an expiry date, the date can be expressed in the following formats: MM/YY and MM/YYYY
  • If used for inputting a date of birth, the date can be expressed in the following formats: DD/MM/YYYY, DD.MM.YYYY, MM,DD,YYYY, YYYY-MM-DD)
  • If used for inputting an IBAN, the IBAN can be expressed in the following format at least: AAXX XXXX XXXX XX
  • Symbols can be selected if a user tries to when editing a previously entered date; the autoformatting doesn't happen until a the caret reaches the end of the text, or if the field losese focus
  • When using the backspace key, any symbols are removed for the customers, they don't have to delete them manually
  • Do not use the HTML5 number, phone or date attributes
  • Do not use this pattern in conjunction with a date picker to add values

Labels

  • Inputting format must always be shown using the form explantory text outside the field

Non-JS requirements and considerations

  • No automatic formatting happens; the field is just a textbox

Placement

  • Follows existing padding rules and form textbox placement

Use cases and exception scenarios

  • It is recommended that the date input is used when requiring standard date entry in order to allow the user the choice of input methods

Accessibility and screen readers

  • Aria-invalid must be used to indicate when an error has occurred with this element
  • Aria-describedby must be used to link any help, instructional and/or error text with the element/form fields

Examples

Interactive example

Code example


<div class="m-form-row [ Modifiers ]">
	<label class="a-label">...</label>
	<div class="m-form-row__content">
		<p class="m-form-row__instruction" id="...">...</p>
		<input type="text" id="..." name="..." pattern="..." class="a-textbox [ Modifiers ]" maxlength="..." data-module="a-textbox-format" data-format="..." data-spacer="..." aria-describedby="..." />
	</div>
</div>
      

Iban textbox

<div class="m-form-row m-form-row--left"> <label class="a-label" for="test-iban">IBAN - " " separator</label> <div class="m-form-row__content"> <p class="m-form-row__instruction" id="inst8">Please use the format IBAN</p> <input type="text" id="test-iban" name="test-iban" class="a-textbox" maxlength="49" pattern="[a-zA-Z]{2}\d{2}[a-zA-Z0-9\s]{14,45}" data-module="a-textbox-format" data-format="iban" data-spacer=" " placeholder="FR76 XXXX XXXX XXXX XXXX XXXX XXX" > </div> </div>

Registration (number plate) textbox

<div class="m-form-row m-form-row--left"> <label class="a-label" for="test-registration"> Registration (number plate) - "-" separator </label> <div class="m-form-row__content"> <p class="m-form-row__instruction" id="inst9"> Please use the french number plate formats </p> <input type="text" id="test-registration" name="test-registration" autocomplete="off" class="a-textbox a-textbox--registration" minlength="9" maxlength="10" pattern="^[a-zA-Z]{2}(-?)\d{3}(-?)[a-zA-Z]{2}$|\d{2,4}(-?)[a-zA-Z]{1,3}(-?)\d{2}$|\d{2,4}(-?)[a-zA-Z]{1,3}(-?)[2][a-bA-B]$" data-module="a-textbox-format" data-format="registrationFr" data-spacer="-" placeholder="AB-123-CD ou 1234-AB-56" > </div> </div>

Development and test

Notes for developers

Line-breaks are automatically removed from the input value

Use the maxlength attribute to specify the maximum length of the value that can be entered; the value should also include the number of spacers required (eg, DD/MM/YYYY would require maxlength="10"; the a-textbox--x-character modifier should include the same character count (so, for the same example as previously, a-textbox--10-character

The pattern attribute must be set on the text input:

  • eg. for DD/MM/YYYY the attibute should have a value of \d{2}\/\d{2}\/\d{4}.
  • eg. for AAXX XXXX XXXX XX the attibute should have a value of [a-zA-Z]{2}\d{2}[a-zA-Z0-9\s]{14,45}.

The data-format refers to the number of characters in each block; supported formats are:

  • "22"
  • "24"
  • "222"
  • "224"
  • "422"
  • "iban"

The data-spacer refers to the spacer character inserted between block; supported spacer characters are:

  • "/"
  • "-"
  • "."
  • ","
  • " "

For more information on sizing the text inputs, please see the textbox page.

Input types

Care should be taken when choosing input field types for various uses, particularly with regard to number entry, as making the wrong selection could prevent users from inputting the required details into a form. For further information, see the form introduction page.

When disabling form elements, do not solely rely on the is-disabled class, but ensure that the disabled attribute is also set on the relevant form element or input. If an entire set of form elements is to be disabled, the is-disabled class and disabled attribute should be added directly to the fieldset instead, and not to each individual element within it. However if only one element from the set is disabled, then the class and attribute should be applied to that element only.

Aria usage for errors and help text

To aid screen reader users in completing forms without error and also with understanding and fixing any errors that have occured, some additional aria attributes are now recommended to be applied across all form fields/rows.

All form row help, explanatory text, form row instructions and form row errors must have a unique id assigned to them and be appropriately linked to either the individual form input or the form row using aria-describedby. This ensures that the help text is read out immediately on focus of the form input/fieldset. When there is more that one item that requires referencing within the aria-describedby attribute the id values must be supplied in the form of a space separated list. For example if there is an error with id of 'err1' and help text with id of 'help1' the value for the aria-describedby attribute would be 'err1 help1'.

All inline form error messages should start with the hidden text 'Error: ' so that when read by out by a screen reader it is immediately clear that the text being read is an error message.

All erroring form fields (or fieldset if a group or radios/checkboxes) must have aria-invalid="true" set on them, ensuring that when a user focus' on an errored form field, the field will be announced as invalid, so the user is then aware that there in an issue that requires fixing.

When linking help or errors, or even adding aria-invalid to the markup careful consideration must be taken as to where it is most appropriate to apply the value. The following rules should be used to decide where the attribute(s) should be applied.

  • Single form field - All attributes should be applied directly to that form field
  • Group of form fields but only one in error or help text applies to only one - Apply the attribute(s) to the only field in error or where the help text applies
  • Group of form field where all in error or the help text applies to all - Apply the attribute(s) to the groups surrounding fieldset (form row)

There may situations where more than one of these rules applies to the overall form row for example a date range where the instruction text applies to both fields so should be linked to the fieldset for the date range but one of the two fields is in error so the error should be linked to just that field not to the fieldset.

Important messages

To help those using assistive technologies, make sure that success/error messages which need to be announced to users have their aria attributes updated to role="alert" and/or aria-live="assertive".

Using autocomplete

Users appreciate when websites save them time by automatically filling common fields like names, email addresses and other frequently used fields, plus it helps to reduce potential input errors, especially on virtual keyboards and small devices.

Browsers use many methods to determine which fields they can auto-populate based on previously specified data by the user, and you can give hints to the browser by providing both the name attribute and the autocomplete attribute on each input element.

Further details can be found in the developer notes on the form introduction page.

Controling the textbox format component programatically

Textbox format can be controlled programatically to set and clear the value. The following methods are available for use:

Method Description
setValue( '55443333' ) Sets the value of the input to the value passed in. This value can be provided both unformatted or pre-formatted.
clear() Clears the value of the textbox.

For example:

document.addEventListener( 'frameworkready', function () {
	FRAMEWORK.require([ 'blocks/a-textbox-format' ], function ( aTextboxFormatModule ) {
		var textboxEl = document.querySelector( '#inputId' ),
			textboxInstance = aTextboxFormatModule.initInstance( textboxEl );		textboxInstance.setValue( '33445555' );
	});
});

Notes for testers

  • Ensure that all form fields have a correctly associated label; this not only provides context to each field for assistive technology users but also a larger clickable area to select the field
  • Clicking / pressing on a label for a single input field should cause the input to gain focus
  • Even though a label might not be visible for some of these fields, it still needs to be present for assistive technologies

Classes overview

The following table gives you a quick overview of the CSS classes that can be applied.

Class Outcome Required Applied to Comments
.a-textbox Styling for the text input Yes input  
.a-textbox--3-character Modifier for restricting the textbox length for 3 characters   .a-textbox maxlength must be set to 3; use for 22 format
.a-textbox--7-character Modifier for restricting the textbox length for 7 characters   .a-textbox maxlength must be set to 7; use for 24 format
.a-textbox--8-character Modifier for restricting the textbox length for 8 characters   .a-textbox maxlength must be set to 8; use for 222 format
.a-textbox--10-character Modifier for restricting the textbox length for 10 characters   .a-textbox maxlength must be set to 10; use for 224 and 422 formats

Keyboard operations

TAB

Tabbing to an input field should make the input clearly visually different so that the focus point on the page is obvious to the user.

Component releases

  • Added on: v4.0.0 (20/07/18)
  • Updated on: v2023.01 (26/04/23)

Latest update:

  • updated: Textbox format can now be controlled programatically to set and clear the value.

Full version history

A full history of changes and enhancements detailing all minor and major updates to the component.

View full version history

Want something new in Framework, or to chat about an issue you're having with it?

Contact the team