简体   繁体   中英

AngularJS problems evaluating regex ng-pattern

I'm using ng-pattern argument in input text to limit input to numeric values:

<input type="text" ng-model="numericField" ng-pattern="/^[0-9]*$/" />

But there is a strange behavior in regex evaluation: starting and ending spaces are ignored...

So if I insert these values (for example) I get different results:

' 123 ' pattern matched

' 123 4343 ' pattern not matched

In my case white spaces are not allowed (in any position of the string).

Update I need to solve the problem also for other inputs allowing char values (ie email)

So how can I solve this?

Why not just use:

<input type="number" ng-model="numericfield" />

html5 behaviour is implemented by angular in older browsers.

You can add ng-trim="false" to your input text:

<input type="text" ng-model="numericField" ng-pattern="/^[0-9]*$/" ng-trim="false" />

Take a look here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM