Phone Number Validation Function
Bob Sneidar
bobsneidar at iotecdigital.com
Mon Sep 11 14:20:27 EDT 2017
Hi all.
Since I needed to write one, I thought I would share this phone number validation function. NOTE: it only works for phone numbers of the following format:
-- pure numbers
nnnnnnn
nnnnnnnnnn
-- formatted numbers
nnn-nnnn
nnn-nnn-nnnn
(nnn) nnn-nnnn
It will also accept a space delimited extension so long as it begins with x, ex or ext.
Anyone who wants to massage it into shape for European or other phone number formats can do so. The range checking is somewhat coarse, because I had difficulty determining what the lowest and highest area code and prefix can be, and also it may change in the future. Enjoy!
Bob S
function isPhoneNum pValue
/*
A phone number is a value whose raw numeric value is between 1000000 and 9999999, or 1000000000 and
9999999999, with an optional numeric value delimited by a space. Additionally, the first and fifth characters
can be ( and ) respectively, in which case the 6th character must be a space and the tenth character must be
a dash.
*/
put pValue into tPhoneNumber -- preserve initial value
put last word of tPhoneNumber into tExtension
if first char of tExtension is "x" or char 1 to 3 of tExtension is "ext" then
delete last word of tPhoneNumber -- lose the extension
else
put empty into tExtension -- it's not a valid extension
end if
-- range check
put cleanAscii(tPhoneNumber, "numbers") into tPhnValue
if tPhnValue > 9999999 then
if tPhnValue < 1000000000 or tPhnValue > 9999999999 then
return false
end if
else
if tPhnValue < 1000000 then
return false
end if
end if
-- pure numbers are OK
if tPhoneNumber = tPhnValue then return true
-- allowed characters
if length(tPhnValue) = 7 then
-- length must be 8 chars and char 4 must be a dash
if length(tPhoneNumber) <> 8 or char 4 of tPhoneNumber is not "-" then
return false
end if
else
-- length must be 14, 1st char must be (, 5th char must be ), 6th char must be space and 10th char must be -
-- or else length must be 12, 4th char must be - and 8th char must be -
if length(tPhoneNumber) <> 14 or length(tPhoneNumber) <> 12 then
return false
end if
if length(tPhoneNumber) is 14 then
if char 1 of tPhoneNumber is not "(" or \
char 5 of tPhoneNumber is not ")" or \
char 6 of tPhoneNumber is not space or \
char 10 of tPhoneNumber is not "-" then
return false
end if
else
if char 4 of tPhoneNumber is not "-" or \
char 8 of tPhoneNumber is not "-" then
return false
end if
end if
end if
return true
end isPhoneNum
More information about the use-livecode
mailing list