validate string to verify it is (probably) a phone number

Answered Question
Jan 12th, 2010
User Badges:

What would I do in expression editor to validate that a string meets the basic constraints for a North American phone number? All I care do at this point is compare the entered string and make sure it matches this pattern: [2-9]XX[2-9]XXXXXX


Thanks much!

Correct Answer by Tanner Ezell about 7 years 2 months ago

I'm curious, where/how are you getting the number?


Either way, assuming you've already validated the length of digits you can skip the need for regex, granted this method is less robust but simpler to implement:


if(phoneNumber.charAt(0) <= 49 || phoneNumber.charAt(3) <= 49)

     True:

          Number is invalid

     False:

          Number is valid.


The way this works is as such: based on the pattern you describe, either the first digit, or the 4th must be between 2 and 9. Based on that, we grab those digits and compare them. The value 49 is the integer value of the character '1', so by testing less than or equal too, you can ensure that the number provided is either 0 or 1



As I said, the code Anthony provided is more robust, you can match on a greater granularity (although I believe his implementation could be improved, no offense Anthony), however if you want to simply ensure they don't input a 0 or 1.


If you'd like to see a table of where all the ASCII string values are, check:


http://www.asciitable.com/

Correct Answer by Anthony Holloway about 7 years 2 months ago

Check this post for the solution (I hope this link still works when they redesign the forums the next time)

https://supportforums.cisco.com/message/1331908#1331908

  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 5 (2 ratings)
Loading.
Correct Answer
Tanner Ezell Tue, 01/12/2010 - 19:45
User Badges:
  • Silver, 250 points or more

I'm curious, where/how are you getting the number?


Either way, assuming you've already validated the length of digits you can skip the need for regex, granted this method is less robust but simpler to implement:


if(phoneNumber.charAt(0) <= 49 || phoneNumber.charAt(3) <= 49)

     True:

          Number is invalid

     False:

          Number is valid.


The way this works is as such: based on the pattern you describe, either the first digit, or the 4th must be between 2 and 9. Based on that, we grab those digits and compare them. The value 49 is the integer value of the character '1', so by testing less than or equal too, you can ensure that the number provided is either 0 or 1



As I said, the code Anthony provided is more robust, you can match on a greater granularity (although I believe his implementation could be improved, no offense Anthony), however if you want to simply ensure they don't input a 0 or 1.


If you'd like to see a table of where all the ASCII string values are, check:


http://www.asciitable.com/

mloraditch Tue, 01/12/2010 - 19:50
User Badges:

Tanner,

I am getting the number from caller input to be used later for a semi-automated callback.


Anthony's method does seem to be more robust, but for my purposes your simpler method will do all I need or care to be able to do.


Thanks to both of you!

Actions

This Discussion