Let Wufoo do the hard work. Sign up for a free account and start making forms the easy way.
Live Demo
Firefox 4+ | Safari 5+ | Safari 4+ | Chrome 10+ | Opera 11+ | IE 10+ | Android 2.3+ |
|---|---|---|---|---|---|---|
The Low Down
Using the pattern attribute, you can declare your own requirements for validation using Regular Expressions.
- Relevant for the
text, search, url, tel, email, andpasswordtypes: the input types that allow for freeform data entry and don’t have predefined patterns the values must match. - The value of the
patternattribute is a regular expression that must match the entire value of the input. - The empty string is considered valid (no misMatch) unless the
requiredattribute is present as well (valueMissing). - The regular expression must match the entire value, not just a section of the value, as if it started with ^
and ended with$, which is different than you might be used to in JavaScript pattern matching. - For
emailinput types, if you include both thepatternandmultipleattributes, remember that the pattern must match the entire value! - Use the
titleattribute to describe what pattern you expect, as per the spec. When thepatternattribute is included, the item’stitleattribute gets special meaning. If there is a patternMismatch during constraint validation , thetitlevalue will be included as content in the error bubble displayed in supporting browsers – as displayed to the left (“Enter 3 characters” was thetitle). That’s why the previous point — describe the pattern you expect, not the purpose of the input — is important to remember. - If the
patternattribute is present, the non-empty value must match the pattern attribute. If the value does not match the regular expression, the patternMismatch constraint validation error message and title will be displayed in the error bubble upon form submission. - Resource of useful patterns
- Android, Safari and Mobile Safari support the
patternattribute, but do not support form validation, so will not prevent an incorrect value from being submitted, but do support the UI pseudoclasses.