简体   繁体   中英

Regular expression ng-pattern angularjs

I have to validate an input field, but I have problems when the user copy and paste something inside the input

This is my code

<input type="text" ng-change="calculate()" ng-pattern="coordsPattern" ng-model="from" class="input-coords" placeholder="(x|y)">

where the coordsPattern is:

$scope.coordsPattern = /^\(?\-?\d{1,3}\|\-?\d{1,3}\)?$/;

the input can take

(158|158)  
-158|158  
(-158|158  

.....etc

but when the user copy and paste the same thing from a different page, depending on browser to browser, the input looks like (158|158) but the pattern is invalid because when copying there are hidden tabs or spaces between chars. for example

((tab)(tab)158(tab)|(tab)(tab)-158(tab)

but in the input text looks like (158|-158 so for the user is a valid input
the input is valid (because in the calculate() function I clean the input from spaces and tabs) but invalid with that pattern and angular doesn't execute the calculate() function.

this one is a copy&paste text which includes hidden tabs

(‭-‭91‬‬|‭-‭18‬‬)

Thank you

EDIT

this is the var_dump of the string

string '(‭-‭91‬‬|‭-‭18‬‬)' (length=33)

it contains special chars! neither tabs or spaces!
maybe I have to find a different solution to validate the input...

$scope.coordsPattern = /^\s*?\(?\s*?\-?\s*?\d{1,3}\s*?\|\s*?\-?\s*?\d{1,3}\s*?\)?\s*?$/;

即使插入了空白字符,这也应该与预期的输入匹配。

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