Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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

share|improve this question

1 Answer 1

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

share|improve this answer
    
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 3 '14 at 11:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.