Form field onblur event issue (really strange!)

465 Views Asked by At

I am currently struggling with a really really strange issue.

  • I have a telephone number form field
  • There's a function that fires onblur and validates whether the value is valid or not
  • Every second time I do a blur, it validates as false, even if the value is correct and the value hasn't changed!

I have set up a small test case: http://jsfiddle.net/U3jwx/2/

Just click into the field and afterwards outside it - multiple times!

How can that be? The value does not even change! I suppose it has something to do with the validation function...

1

There are 1 best solutions below

4
On BEST ANSWER

reset your regular expression's lastIndex to 0 between calls.

var validatePhoneNumber = function(val) {        
        var re = /^\+([0-9]{1,3})([\-\s]{0,1})([0-9]{2,6})([\-\s]{0,1})([0-9\-\s]{0,})$/gi;
        re.lastIndex = 0;
        return re.test(val);
};