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);
Comments
Post a Comment