all
alt
ch
dir
id
min
rel
top
url
URL
urn
Expand Minimize

pattern attribute | pattern property

Gets or sets a string containing a regular expression that the user's input must match.

This property is read/write.

HTML5 A vocabulary and associated APIs for HTML and XHTML, Section 4.10.7.3.9Internet Explorer 10

 

Syntax

HTML
<element pattern="ptr" ... >

 

JavaScript
object.pattern = ptr
ptr = object.pattern

 

Property values

Type: DOMString

String containing the regular expression used to match user input.

Standards information

Remarks

Several generic messages are displayed for a variety of validation errors. If you use a title attribute on an input element, it will both be shown as alt text for the field, as well as be appended to the generic error message.

The following example shows a ZIP code number format.


<!DOCTYPE html>

<html>
<head>
  <meta charset="utf-8" />
  <title>pattern</title>
</head>
<body>
  <form>
    <label>
      Zip: <input type="text" 
                  pattern="(^\d{5}$)|(^\d{5}-\d{4}$)" 
                  placeholder="5 or 9 digit zip" 
                  title="5 or 9 digit zip" 
                  required />
    </label>
    <input type="submit" />
  </form>
</body>
</html>

See also

HTMLInputElement

 

 

Show:
© 2014 Microsoft