0

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...

rdonatoiop
  • 1,185
  • 1
  • 14
  • 28
peppeocchi
  • 814
  • 1
  • 9
  • 22

1 Answers1

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

This should match the expected input even when there are whitespace characters inserted.

Anatol
  • 191
  • 6
  • This one doesn't work with the copy&paste. I also tried to change \s in \t, but still not working. Try to copy this string in a text editor ‎‭(‭-‭91‬‬|‭-‭18‬‬) it contains hidden chars (I think tabs or spaces, but maybe special chars) – peppeocchi Apr 03 '14 at 11:56