HTML <input> patternAttribute

HTML input 标签参考手册HTML <input> Tag

Examples

An input field that can only contain three letters (no numbers or special characters):

<form action="demo_form.html">
Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}" title="Three letter country code">
<input type="submit">
</form>

Try it Yourself »

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 10, Firefox, Opera, and Chrome support the pattern attribute.

Note:Safari or Internet Explorer 9 and earlier versions do not support the pattern attribute of the <input> tag.


Definition and Usage

The pattern attribute specifies a regular expression used to validate the value of an <input> element.

Note:The pattern attribute applies to the following input types: text, search, url, tel, email, and password.

Tip:Please use the globaltitleattribute to describe the pattern to help users.

Tip:In our JavaScript tutorial, you can learn more aboutregular expressionsknowledge


Differences Between HTML 4.01 and HTML5

The pattern attribute is a new attribute in HTML5.


Syntax

<input pattern="regexp">

Attribute Values

Value Description
regexp Specifies a regular expression used to validate the value of an <input> element.


HTML input 标签参考手册HTML <input> Tag Other Extensions