Sign in to follow this  
Followers 0
Bert

Phone number check

19 posts in this topic

I have a string in a webpage that I need to make sure it only has phone numbers formatted in a certain way. For example it may have in the field (among other things):

  • (555)333-1111
  • 555*333*2222
  • 15553332222ext1234
  • 555.333.2222
  • 1-555-333-2222

I just need to get the area code, the exchange and the last 4 digits. I then need to split the phone number so the area code is assigned a variable, the exchange has a variable, and the last 4 digits has a variable. I do not need the extension if they put that in.

$area = 555

$exch = 333

$last = 2222

My problem is how do I approach this one. :D

I've spent an hour searching the forum and the help file looking for an example, but I haven't found anything that is close to what I need.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi,

maybe this is what you want, quick and dirty:

Global $area, $exch, $last
$string1 = "(555)333-1111"
$string2 = "555*333*2222 "
$string3 = "15553332222ext1234"
$string4 = "555.333.2222"
$string5 = "1-555-333-2222"

_phone ($string1)
MsgBox (0,"", $area & " " & $exch & " " & $last)
_phone ($string2)
MsgBox (0,"", $area & " " & $exch & " " & $last)
_phone ($string3)
MsgBox (0,"", $area & " " & $exch & " " & $last)
_phone ($string4)
MsgBox (0,"", $area & " " & $exch & " " & $last)
_phone ($string5)
MsgBox (0,"", $area & " " & $exch & " " & $last)

Func _phone ($string)
    $count = 0
    $last = ""
    $area = ""
    $exch = ""
    For $i = 1 To StringLen ($string)
        $char = StringMid ($string, $i, 1)
        Select 
            Case $char = "1" And $i = 1
                ContinueLoop
            Case $char = "("
                ContinueLoop
            Case $char = ")"
                ContinueLoop                
            Case $char = "-"
                ContinueLoop
            Case $char = "*"
                ContinueLoop
            Case $char = "."
                ContinueLoop                
            Case Else
                $count += 1
                Switch $count
                    Case 1 to 3
                        $area &= $char
                    Case 4 To 6
                        $exch &= $char
                    Case 7 To 10 
                        $last &= $char
                    Case 11
                        ExitLoop
                EndSwitch
        EndSelect
    Next
EndFunc

;-))

Stefan

Edited by 99ojo

Share this post


Link to post
Share on other sites

99ojo,

I was thinking along the same lines :-)

...except using StringSplit before the for/next loop and Int() to determine number.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Volly,

A slightly different version using StringRegExpReplace:

Global $area, $exch, $last
$string1 = "(555)333-1111"
$string2 = "555*333*2222 "
$string3 = "15553332222ext1234"
$string4 = "555.333.2222"
$string5 = "1-555-333-2222"

_phone ($string1)
MsgBox (0,"", $area & " " & $exch & " " & $last)
_phone ($string2)
MsgBox (0,"", $area & " " & $exch & " " & $last)
_phone ($string3)
MsgBox (0,"", $area & " " & $exch & " " & $last)
_phone ($string4)
MsgBox (0,"", $area & " " & $exch & " " & $last)
_phone ($string5)
MsgBox (0,"", $area & " " & $exch & " " & $last)

Func _phone($sNumber)

    $sNumber = StringRegExpReplace($sNumber, "ext.*", "")

    $Number = StringRegExpReplace($sNumber, "\D", "")

    If StringLeft($Number, 1) = "1" Then $Number = StringTrimLeft($Number, 1)

    $area = StringMid($Number, 1, 3)
    $exch = StringMid($Number, 4, 3)
    $last = StringMid($Number, 7, 4)

EndFunc

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

The thing one needs to keep in mind is the examples I showed you: the original string COULD be formatted in any of the examples I have shown, but there could be many other ways of someone putting in a phone number. I have to account for any screwy way someone could think of when they type in their phone number. That is what is making my brain hurt trying to wrap my mind around it. I'm figuring any letters, punctuation, or spaces have to be accounted for. I also have to include the fact they may add a "1" at the beginning of the number.

Share this post


Link to post
Share on other sites

Volly,

2 questions, if I may:

1. Are you only dealing with US numbers and if so are all US numbers of the "555-111-2222" flavour (other nations have variable number formats!) with the possible addition of a leading "1"?

2. What would be the most likely inputs for "extension"? You used "ext", a single "x" is quite common in the UK.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Do you have access to the page? I would use PHP preg_match or js in some way to make sure the right format is input right off, along with text before the field specifying a preferred format.It would probably be alot easier to split up that string if it was already in a known format.<br>

Edited by snowmaker

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I'm only dealing with US numbers. The problem is the user is entering in the text on a web form that will accept any text in the field in question (letters, number, spaces, punctuation) . I have to capture that information, reformat it, then put it in a different web form. I'm dealing with a vendor web site (no I can't grant you access to it) and a internal web site my users use to submit tickets. The vendor only allow for the entering of data through the web interface they provide. Seeing how I have folks entering in tickets using a different system, I'm having to make a interface to pass the information from one to the other. I've got everything else worked out except the phone number issue.

I hope this all makes sense.

I'm thinking after looking at this function:

Func _phone($sNumber)

    $sNumber = StringRegExpReplace($sNumber, "ext.*", "")

    $Number = StringRegExpReplace($sNumber, "\D", "")

    If StringLeft($Number, 1) = "1" Then $Number = StringTrimLeft($Number, 1)

    $area = StringMid($Number, 1, 3)
    $exch = StringMid($Number, 4, 3)
    $last = StringMid($Number, 7, 4)

EndFunc

I should be able to add to the $sNumber to include any punctuation, letters, or spaces. That should clean it up.

Edit - Did some testing, think the above example does the trick. Thanks!

Edited by Volly

Share this post


Link to post
Share on other sites

Volly,

You might want to add a few options to the "ext.*" pattern such as:

"(?i)(ext.*|extension.*|x.*)"

and any others you can think of! :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

You can try these functions too.

#include<array.au3> ;; For _ArrayDisplay only

Local $aNums[6]
$aNums[0] = "(555)333-1111"
$aNums[1] = "555*333*2222 "
$aNums[2] = "15553332222ext1234"
$aNums[3] = "555.333.2222"
$aNums[4] = "1-555-222-3333"
$aNums[5] = "1+555+333+2222"

For $i = 0 To Ubound($aNums) -1
    $aRtn = _PhoneNumToArray($aNums[$i])
    _ArrayDisplay($aRtn, "Array Result")
    MsgBox(0, "String Result", _PhoneNumToString($aNums[$i]))
Next

Func _PhoneNumToArray($sNum)
    $sSRE = "(\d{3})(\d{3})(\d{4})"
    $sNum = StringRegExpReplace($sNum, "([a-z]+\d+)", "")
    $sNum = StringStripWS(StringRegExpReplace($sNum,"\D", ""), 2)
    $sNum = StringRight($sNum, 10)
    Local $aSRE = StringRegExp($sNum, $sSRE, 1)
    If Not @Error Then Return $aSRE
EndFunc

Func _PhoneNumToString($sNum)
    $sNum = StringRegExpReplace($sNum, "([a-z]+\d+)", "")
    $sNum = StringStripWS(StringRegExpReplace($sNum,"\D", ""), 2)
    $sNum = StringRight($sNum, 10)
    Return StringMid($sNum,1,3) & "-" & StringMid($sNum, 4, 3) & "-" & StringMid($sNum, 7)
EndFunc
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

If you need to get the extension as well you can do it with this.

Func _PhoneExt($sNum)
   Local $sExt = StringRegExpReplace($sNum, "(.+[[:alpha:]]|\.|\s)+(\d+)$", "$2")
   If @Extended Then Return $sExt
   Return ""
EndFunc

I would suggest that you do it before running either of the other 2 functions that I gave you though.

BTW: The last 2 functions I gave you assume an Area code was given. If that is not always the case then let me know and I can change them.

EDIT: Modified the Return values.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

See if this works:

#include <array.au3>
Global $s_string = "I am a text with 1(210)555-1212 phone number in it"
Global $a_format = _Format_PhoneNumber($s_string)
_ArrayDisplay($a_format)

Func _Format_PhoneNumber($s_text)
    Local $s_pattern = "^(.*?)(\d{3})(\d{3})(\d{4})((?!\d).*?)\z"
    Local $s_format = StringRegExpReplace(StringRegExpReplace($s_text, "\W", ""), $s_pattern, "$2-$3-$4")
    Return StringSplit($s_format, "-")
EndFunc

Edit:

If extension is important, you could do something like:

#include <array.au3>
Global $s_string = "I am a text with 1(210)555-1212ext phone number in it"
Global $a_format = _Format_PhoneNumber($s_string)
_ArrayDisplay($a_format, "No Extension")

$s_string = "I am a text with 1(210)555-1212ext1839 phone number in it"
$a_format = _Format_PhoneNumber($s_string)
_ArrayDisplay($a_format, "With Extension")

Func _Format_PhoneNumber($s_text)
    Local $s_pattern = "^(.*?)(\d{3})(\d{3})(\d{4})((?!\d)ext)(\d+)(.*?)\z"
    Local $s_format = StringRegExpReplace(StringRegExpReplace($s_text, "\W", ""), $s_pattern, "$2-$3-$4-$6")
    If @extended Then Return StringSplit($s_format, "-")
    $s_pattern = "^(.*?)(\d{3})(\d{3})(\d{4})((?!\d).*?)\z"
    $s_format = StringRegExpReplace(StringRegExpReplace($s_text, "\W", ""), $s_pattern, "$2-$3-$4")
    If @extended Then Return StringSplit($s_format, "-")
    Return SetError(1, 0, "")
EndFunc

You would obviously check $a_format[0] to see if it was 3 or 4, 4 would be with an extension.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Question. Is this form on your web site? It looks to me like the form could use some re-design by splitting the phone number into 3 fields

Area Code

Number

Extension

That would make it extremely easy to work with.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

... You would obviously check $a_format[0] to see if it was 3 or 4, 4 would be with an extension.

I've seen 2, 3, 4 & 5 digit extensions... it depends on the size of the PBX, its software and who set it up.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

I've seen 2, 3, 4 & 5 digit extensions... it depends on the size of the PBX, its software and who set it up.

Hmm, you're saying what I wrote wouldn't handle multiple digit extensions? Pretty sure it will.

Edit:

If you're confused on the "3 or 4", that's not the number of indexes to be accessed in the array, that means:

UBound of 3 is only number ( 10 digit )

UBound of 4 is number and extension

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Actually, I did this, and it works perfectly. (thats unless I missed something. I've always had trouble understanding StringRegExp)

$sNumber= StringRegExpReplace($sNumber, "abcdefghijklmnopqrstuvwxyz`~!@#$%^&()-_<>?/;:[]{}\|,.*", "")
    $Number = StringRegExpReplace($sNumber, "\D", "")
    If StringLeft($Number, 1) = "1" Then $Number = StringTrimLeft($Number, 1)
    $area = StringMid($Number, 1, 3)
    $exch = StringMid($Number, 4, 3)
    $code = StringMid($Number, 7, 4)

Edit: :) I didn't think about the string length.

Edited by Volly

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Actually, I did this, and it works perfectly. (thats unless I missed something. I've always had trouble understanding StringRegExp)

$sNumber= StringRegExpReplace($sNumber, "abcdefghijklmnopqrstuvwxyz`~!@#$%^&()-_<>?/;:[]{}\|,.*", "")
    $Number = StringRegExpReplace($sNumber, "\D", "")
    If StringLeft($Number, 1) = "1" Then $Number = StringTrimLeft($Number, 1)
    $area = StringMid($Number, 1, 3)
    $exch = StringMid($Number, 4, 3)
    $code = StringMid($Number, 7, 4)

Edit: :) I didn't think about the string length.

You assume (and it might be right) that the 1st digit of an area code can't be "1". Also, if that's true (that an area code can start with 1), and if there's an extention, you've removed all the correct data to distinguish so... so you're return will be incorrect. Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

In case you change your mind, here it is all rolled into a single package.

#include<array.au3> ;; For _ArrayDisplay only

Local $aNums[6]
$aNums[0] = "(555)333-1111"
$aNums[1] = "555*333*2222 "
$aNums[2] = "15553332222ext1234"
$aNums[3] = "555.333.2222"
$aNums[4] = "1-555-222-3333"
$aNums[5] = "1+555+333+2222"

For $i = 0 To Ubound($aNums) -1
    $aRtn = _Format_PhoneNumber($aNums[$i])
    If IsArray($aRtn) Then
        _ArrayDisplay($aRtn, "Array Result")
    Else
        MsgBox(0, "String Result", $aRtn)
    EndIf
Next

;;  This func is modified slightly from SmOke_N's to allow
;;  either a string or an array as the return value
Func _Format_PhoneNumber($s_text, $aRtn = 1, $ext = 1)
    Local $sExt = ""
    If $ext = 1 Then $sExt = _PhoneExt($s_Text)
    Local $s_pattern = "^(.*?)(\d{3})(\d{3})(\d{4})((?!\d).*?)\z"
    Local $s_format = StringRegExpReplace(StringRegExpReplace($s_text, "\W", ""), $s_pattern, "$2-$3-$4") & $sExt
    If $aRtn = 1 Then Return StringSplit($s_Format, "-", 2)
    Return $s_Format & $sExt
EndFunc

Func _PhoneExt($sNum)
   Local $sExt = StringRegExpReplace($sNum, "^(?:.+[[:alpha:]]|\.|\s)(\d+)$", "$1")
   If @Extended Then Return "-" & $sExt
   Return ""
EndFunc

Edit: I should have told you what the params were for that call

$s_text is obvious

$aRtn = If 1 Return an array else return a string

$ext = If 1 include the phone extension else don't include it.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Hmm, you're saying what I wrote wouldn't handle multiple digit extensions? Pretty sure it will.

Edit:

If you're confused on the "3 or 4", that's not the number of indexes to be accessed in the array, that means:

UBound of 3 is only number ( 10 digit )

UBound of 4 is number and extension

I see now - I just misread that line of info.

Regex does it again :-)


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0