Skip to main content

Masked Text Field

Description

A text field that forces the user input text to follow a defined pattern. The enforced pattern is called a mask. In addition to limiting the input, masks also help with how the field is displayed. Examples include zip codes and phone numbers. For a phone number the ( ) and - will be added to the field without the user needing to type those characters.

Props

NameTypeRequiredDescription
maskobject, array, or functionyesValid mask array. Custom or from utils/masks.js
namestringyesName of the field will be the Object key of the key/value pair form payload
labelstringyesDescriptive label of the input
placeholderstringnoPlaceholder text used inside the input field to provide hints to the user
isDisabledbooleannoDisabled property to disable the input if true
size'auto' or a number 1 - 12noSize of the input given full-width is 12.
onBlurfunctionnoCustom onBlur event callback
onChangefunctionnoCustom onChange event callback
startAdornmentnodenoAdornment that appears at the start of the input
endAdornmentnodenoAdornment that appears at the end of the input
typestringnoDefines the input type for the text field. Must be a valid HTML5 input type
InputPropsobjectnoProps applied to the Input element
inputPropsobjectnoAttributes applied to the input element
muiFieldPropsobjectnoAny valid prop for MUI text input child component https://material-ui.com/api/text-field/#props
stripNonNumericbooleannoThe submitted value from the input will have all non-numeric characters removed

Masks

For convenience several common masks are provided.

  • phone: US Phone numbers **Will allow invalid phone numbers see note below
  • zip: US address zip codes
  • currency: a number only mask that allows decimal values.
  • percent: a number mask that adds a % as a suffix
  • email: provided from the text-mask module
  • date: a mask to allow the date form of MM/DD/YYYY
  • ssn: a mask to include the dashes in the xxx-xx-xxxx form of social security numbers.
note

The included phone mask is to get the correct format not to force a valid phone number. Historically it had prevented a 1 in the fourth position. For example: (555) 155-5555

For US phone numbers the fourth digit cannot be a one. Use Yup to validate your phone numbers and do not rely on the mask for validation.

Example

Using the phone mask for a fax number field:

import {
SQFormMaskedTextField,
MASKS
} from '@selectquotelabs/sqform

/* omitted code for brevity */

<SQFormMaskedTextField
name="faxNumber"
label="Fax number"
size={12}
mask={MASKS.phone}
/>