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)

https://plnkr.co/edit/3ove6qiwgjozub3hyfq5?p=preview

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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -