javascript - ng-pattern allows '+' character when regex limits to digits -
am using regex /^[0-9]+$/ limit input of text box accept numbers. it's working fine when types +124 it's not setting text box invalid.
<form name="myform" novalidate> <input type="number" ng-model="age" name="age" ng-pattern="/^[0-9]+$/" /> <h3>valid status : {{myform.age.$valid}}</h3> </form>
input: 123 output: myform.age.$valid - true
input: -123 output: myform.age.$valid - false
input: +123 output: myform.age.$valid - true (shouldn't true)
if want make pattern work, need set type
attribute text
. since want let users type only digits input field, add onkeypress
validation:
<input type="text" onkeypress="return (event.charcode == 8 || event.charcode == 0) ? null : event.charcode >= 48 && event.charcode <= 57" ng-model="age" name="age" ng-pattern="/^[0-9]+$/" />
actually, won't need ng-pattern regex then.
Comments
Post a Comment