javascript - Invalid regular expression in HTML5 input pattern -
i need use regular expression in html input (taken regular expression list of items separated comma or comma , space):
[^,\s][^\,]*[^,\s]*
so set in input:
<input type="text" class="form-control" id="input" name="input" data-ng-model="mymodel" pattern="[^,\s][^\,]*[^,\s]*">
but error in console:
pattern attribute value [^,\s][^\,]*[^,\s]* not valid regular expression: uncaught syntaxerror: invalid regular expression: /[^,\s][^\,]*[^,\s]*/: invalid escape
what wrong regex?
i'm using angular 1.5 btw.
you need remove escaping backslash before comma , use
pattern="[^,\s][^,]*[^,\s]*"
it necessary since ff , chrome compile pattern
regex using es6 regex syntax specifications, , use /u
modifier when compiling regexp object. lays restrictions on pattern. in case, regular symbol, not special metacharacter, comma, escaped inside character class. thus, removing escape solves issue.
input:valid { color: black; border: 5px solid #dadadada; border-radius: 7px; } input:invalid { color: navy; outline: none; border-color: #ff1050; box-shadow: 0 0 10px #ff0000; }
<form> <input type="text" class="form-control" id="input" name="input" data-ng-model="mymodel" pattern="[^,\s][^,]*[^,\s]*"> </form>
Comments
Post a Comment