01-12-2010 05:50 PM - edited 03-14-2019 05:05 AM
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!
Solved! Go to Solution.
01-12-2010 06:25 PM
Check this post for the solution (I hope this link still works when they redesign the forums the next time)
01-12-2010 07:45 PM
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:
01-12-2010 06:25 PM
Check this post for the solution (I hope this link still works when they redesign the forums the next time)
01-12-2010 07:45 PM
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:
01-12-2010 07:50 PM
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!
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide