I am trying to validate and format North American phone numbers They should always be ten digits in the format (xxx) xxx-xxxx.
Here is my code:
// get input without special characters
rawno = input.Cell_Number.getAlphaNumeric();
// remove letters
rawno = rawno.removeAllAlpha();
// get number of digits
noln = rawno.length();
if (noln != 10)
{
alert("Number must be ten digits.\nYou entered " + noln + " digits.\nPlease re-enter number.");
}
else
{
areacode = rawno.subString(0,2);
etc.
But the substring only returns the first two digits, not the first three.
Similarly...
exchange = rawno.subString(3,5);
... only returns two digits.
Is there a bug in the substring function? If yes, is there a workaround?
------------------------------------------------------
Later....
I found a workaround: just make the ending index number 1 higher than it actually is. Thus...
areacode = rawno.subString(0,3);
... returns the first three digits, and ...
exchange = rawno.subString(3,6);
... returns the next three. And so on.
Any plans to fix this?