Input Text pattern Attribute

Definition and Usage

pattern Attribute sets or returns the value of the pattern attribute of the text field.

The HTML pattern attribute specifies a regular expression used to check the value of the text field.

Tip:Please use global HTML title Attribute or DOM title Use the attribute to describe the pattern to help users.

See also:

JavaScript Tutorial:JavaScript Regular Expression

JavaScript Reference Manual:JavaScript RegExp Object

HTML Reference Manual:HTML <input> pattern Attribute

Example

Example 1

Get the pattern attribute value of the text field:

var x = document.getElementById("myText").pattern;

Try it yourself

Example 2

Set the pattern of the text field to only contain numbers from 1 to 3:

document.getElementById("myText").pattern = "[1-3]";

Try it yourself

Syntax

Return the pattern attribute:

textObject.pattern

Set the pattern attribute:

textObject.pattern = regexp

Attribute value

Value Description
regexp Specify the regular expression for checking the value of the text field.

Technical Details

Return Value: A string value representing the regular expression.

Browser Support

The numbers in the table indicate the first browser version that fully supports this attribute.

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Supported 10.0 Supported Not Supported Supported