Attribute: Pattern

The pattern attribute specifies a regular expression that the <input> element's value is checked against. The pattern attribute works with the following input types: text, search, url, tel, email, and password.

Tip: Use the global title attribute to describe the pattern to help the user. (I don't yet understand the relationship between title and pattern attributes...)

If I take the Javascript tutorial, perhaps regular expressions would be better understood. So far, I believe regular expressions are a sequence of characters that forms a search pattern. The search pattern can be used for text search and text replace operations. ... right now, that is also confusing for me!

This example is an input field that can contain only three letters (no numbers or special characters):

HTML file: Displayed by browser:
<form action="http://www.w3schools.com/html/action_page.php">
Country code:
<input type="text" name="country_code"
pattern="[A-Za-z]{3}"
title="Three letter country code">
<input type="submit">
</form>
Country code:

This attribute is compatible with most major browsers, except Safari.

Back button Table of Contents Next button