On Input - Avoiding Infinite Loop

On Input - Avoiding Infinite Loop

I have a nice routine to validate a UK phone number, but I want to update the field with the reformatted number (spaces removed) as part of the routine, which just sends me in an 'on input' infinite loop ?

Any suggestions ?

Thanks

Neil

  1. // Set RegEx Variable Match Strings
  2. UK_Phone_RX = ("^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$");
  3. UK_Mobile_RX = ("^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$");
  4. // Strip out ALL spaces from the number
  5. e = input.Your_Phone_Number.replaceAll(" ","",false);
  6. Error = 0;
  7. // Check if Valid UK Landline
  8. if (!e.matches(UK_Phone_RX))
  9. {
  10.     Error = 1;
  11. }
  12. // Check if Valid UK Mobile - only check if it failed the Landline test 
  13. if (Error  =  1)
  14. {
  15.     if (!e.matches(UK_Mobile_RX))
  16.     {
  17.         Error = 1;
  18.     }
  19.     else
  20.     {
  21.         Error = 0;
  22.     }
  23. }
  24. // Alert if number not valid
  25. if (Error  =  1)
  26. {
  27.     alert("Sorry " + e + " does not appear to be a valid phone number");
  28. }
  29. else
  30. {
  31.     input.Valid_Number = true;
  32. }