Phone Number Validation Function

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Phone Number Validation Function

Sannyasin Brahmanathaswami via use-livecode
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
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Phone Number Validation Function

Sannyasin Brahmanathaswami via use-livecode
WHOOPS! Change this line to
     if length(tPhoneNumber) <> 14 AND length(tPhoneNumber) <> 12 then

> On Sep 11, 2017, at 11:20 , Bob Sneidar via use-livecode <[hidden email]> wrote:
>
>      if length(tPhoneNumber) <> 14 or length(tPhoneNumber) <> 12 then


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Phone Number Validation Function

Sannyasin Brahmanathaswami via use-livecode
In reply to this post by Sannyasin Brahmanathaswami via use-livecode
Thanks Bob.

JB


> On Sep 11, 2017, at 11:20 AM, Bob Sneidar via use-livecode <[hidden email]> wrote:
>
> 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
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Phone Number Validation Function

Sannyasin Brahmanathaswami via use-livecode
Read this:
https://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation

You can then filter by these Regular Expressions (RegEx) really simply.
More importantly, it is one or two lines of code instead of 53.

Sean Cole
*Pi Digital Productions Ltd*

'Don't try to think outside the box. Just remember the truth: There is no
box!'
'For then you realise it is not the box you are trying to look outside of,
but it is yourself!'

eMail Ts & Cs <http://pidigital.co.uk/emailTCs.rtf>   Pi Digital
Productions Ltd is a UK registered limited company, no. 5255609

On 11 September 2017 at 20:04, JB via use-livecode <
[hidden email]> wrote:

> Thanks Bob.
>
> JB
>
>
> > On Sep 11, 2017, at 11:20 AM, Bob Sneidar via use-livecode <
> [hidden email]> wrote:
> >
> > 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
> > _______________________________________________
> > use-livecode mailing list
> > [hidden email]
> > Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> > http://lists.runrev.com/mailman/listinfo/use-livecode
> >
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Phone Number Validation Function

Sannyasin Brahmanathaswami via use-livecode
There are currently 342 expressions on the regexlibrary:
http://www.regexlib.com/Search.aspx?k=phone&AspxAutoDetectCookieSupport=1

Some eliminate erroneous numbers too like those begining with 123 or 555
and so on.

Sean Cole
*Pi Digital Productions Ltd*
www.pidigital.co.uk
+44(1634)402193
+44(7702)116447
'Don't try to think outside the box. Just remember the truth: There is no
box!'
'For then you realise it is not the box you are trying to look outside of,
but it is yourself!'

eMail Ts & Cs <http://pidigital.co.uk/emailTCs.rtf>   Pi Digital
Productions Ltd is a UK registered limited company, no. 5255609

On 11 September 2017 at 23:33, Sean Cole (Pi) <[hidden email]> wrote:

> Read this:
> https://stackoverflow.com/questions/123559/a-
> comprehensive-regex-for-phone-number-validation
>
> You can then filter by these Regular Expressions (RegEx) really simply.
> More importantly, it is one or two lines of code instead of 53.
>
> Sean Cole
> *Pi Digital Productions Ltd*
>
> 'Don't try to think outside the box. Just remember the truth: There is no
> box!'
> 'For then you realise it is not the box you are trying to look outside of,
> but it is yourself!'
>
> eMail Ts & Cs <http://pidigital.co.uk/emailTCs.rtf>   Pi Digital
> Productions Ltd is a UK registered limited company, no. 5255609
>
> On 11 September 2017 at 20:04, JB via use-livecode <
> [hidden email]> wrote:
>
>> Thanks Bob.
>>
>> JB
>>
>>
>> > On Sep 11, 2017, at 11:20 AM, Bob Sneidar via use-livecode <
>> [hidden email]> wrote:
>> >
>> > 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
>> > _______________________________________________
>> > use-livecode mailing list
>> > [hidden email]
>> > Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> > http://lists.runrev.com/mailman/listinfo/use-livecode
>> >
>>
>>
>> _______________________________________________
>> use-livecode mailing list
>> [hidden email]
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
>
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Phone Number Validation Function

Sannyasin Brahmanathaswami via use-livecode
In reply to this post by Sannyasin Brahmanathaswami via use-livecode
Thanks Sean, but reading through that article, I think it makes the arguement for NOT using RegEx, seeing all the arguments back and forth about what is wrong with each other's regex, and one guy pooh poohing all of them in favor of a procedural approach.

My goal is not to simply allow certain characters and numbers (I have a function that does that already) of a given length, but to enforce data consistency across my apps. That means that while certain numbers are acceptably formatted phone numbers according to some international standard, having +1 at the beginning for example, I don't necessarily want that. Periods are valid delimiters, but I don't want that either.

Now the google api looks promising, but if I have to tie it to a google account and active internet connection, I will probably stick to rolling my own.

Bob S


> On Sep 11, 2017, at 15:33 , Sean Cole (Pi) via use-livecode <[hidden email]> wrote:
>
> Read this:
> https://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation
>
> You can then filter by these Regular Expressions (RegEx) really simply.
> More importantly, it is one or two lines of code instead of 53.
>
> Sean Cole


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Phone Number Validation Function

Sannyasin Brahmanathaswami via use-livecode
Hi Bob,

Last year I released a sample library for base functions like this:

https://github.com/renegadesteve/rsIsValid <https://github.com/renegadesteve/rsIsValid>

You can use the LCS version, or build a widget library for your version of LC 9.

It does use RegEx, but you can Sean said, there are many ways to do what you are looking to do.

Since it seems that you are looking for consistency across you app, however you get there, it can be done in multiple ways. RegEx is a valid way to do.

As always, YMMV.

Best,

Steve MacLean

> On Sep 11, 2017, at 6:48 PM, Bob Sneidar via use-livecode <[hidden email]> wrote:
>
> Thanks Sean, but reading through that article, I think it makes the arguement for NOT using RegEx, seeing all the arguments back and forth about what is wrong with each other's regex, and one guy pooh poohing all of them in favor of a procedural approach.
>
> My goal is not to simply allow certain characters and numbers (I have a function that does that already) of a given length, but to enforce data consistency across my apps. That means that while certain numbers are acceptably formatted phone numbers according to some international standard, having +1 at the beginning for example, I don't necessarily want that. Periods are valid delimiters, but I don't want that either.
>
> Now the google api looks promising, but if I have to tie it to a google account and active internet connection, I will probably stick to rolling my own.
>
> Bob S
>
>
>> On Sep 11, 2017, at 15:33 , Sean Cole (Pi) via use-livecode <[hidden email]> wrote:
>>
>> Read this:
>> https://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation
>>
>> You can then filter by these Regular Expressions (RegEx) really simply.
>> More importantly, it is one or two lines of code instead of 53.
>>
>> Sean Cole
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode