Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the html5 "pattern" attribute work in every browser?

Tags:

html

Does html5 pattern attribute work in every browser or not? If doesn't work which technique should I use to work in every browser?

like image 360
pico Avatar asked Feb 13 '12 04:02

pico


People also ask

What does HTML5 pattern attribute do?

The pattern attribute specifies a regular expression the form control's value should match. If a non- null value doesn't conform to the constraints set by the pattern value, the ValidityState object's read-only patternMismatch property will be true.

What is the pattern attribute used for?

Overview. In HTML, the pattern attribute is used to specify the regular expression on which we check the pattern of input values. This attribute works with the <input> elements. We use this attribute with the following input types: text, email, date, password, etc.

How do I add special characters to a pattern in HTML?

After a bit of trial and error, turns out you can use pattern="^[\p{L}]${1,25} to allow ASCII letters and Unicode variants, accented letters and special characters.


1 Answers

No, it doesn't. Though you can use JavaScript to get the attribute and validate it that way.

if ( ! input.hasOwnProperty('pattern') 
     && ~input.value.search(input.pattern)) {
   // Valid input field for browsers which don't support `pattern` attribute.
}

jsFiddle.

like image 102
alex Avatar answered Sep 20 '22 12:09

alex