cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
551
Views
0
Helpful
3
Replies

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

mloraditch
Level 7
Level 7

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!

2 Accepted Solutions

Accepted Solutions

Anthony Holloway
Cisco Employee
Cisco Employee

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

View solution in original post

Tanner Ezell
Level 4
Level 4

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/

Tanner Ezell www.ctilogic.com

View solution in original post

3 Replies 3

Anthony Holloway
Cisco Employee
Cisco Employee

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

Tanner Ezell
Level 4
Level 4

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/

Tanner Ezell www.ctilogic.com

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!

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: