Using the pattern Attribute

You use the pattern attribute to validate the value entered into an input field against a regular expression pattern. For example, the following HTML form validates the Social Security number against a regular expression:

<form>    <div>        <label>            Social Security Number:            <input id="ssn"                required                pattern="^\d{3}-\d{2}-\d{4}$"                title="###-##-####" />        </label>    </div>    <div>        <input type="submit" />    </div></form>

Notice that the ssn field has both a required and pattern attribute. The pattern attribute is not triggered unless you enter a value.

Notice, furthermore, that the ssn field includes a title ...

Get Windows® 8.1 Apps with HTML5 and JavaScript Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.