javascript - Why does this simple regex work everywhere except html5 input pattern attribute? -


this question has answer here:

this html input colours red indicate pattern has not matched when value in input "1".

var inp = document .createelement ('input'); inp .pattern = '^\d+\.?\d*$'; document .getelementbyid ("foo") .appendchild (inp); 

the regex /^\d+\.?\d*$/ matches "1" when test elsewhere. looks should too.

why form element fail match? (firefox.)

string representations, son.

your usage of pattern attribute correct in using string literals. mdn:

the regular expression language same javascript's. pattern not surrounded forward slashes.

however, in string literals have escape \ before slash gets own li'l interpretation in string literal sense. if escape it, seen actual slash , then whole pattern recognised regexp engine special character.

var inp = document.createelement('input'); inp.pattern = '^\\d+\\.?\\d*$'; document.getelementbyid("foo").appendchild(inp); 

https://jsfiddle.net/qatt3rwt/4/


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -