Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression for phone number?

Status
Not open for further replies.

bitbrain

Programmer
Jun 10, 1999
159
US
Does anyone have a regular expression for editing a phone number?<br>
<br>
Thanks in advance!
 
what do you need to do to the phone number?<br>
<br>
-ml<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
I have an input form that includes a phone number field. I want to edit it for validity. Phone numbers could be entered as:<br>
(203) 555-1212<br>
or<br>
203.555.1212<br>
etc.<br>
<br>
I guess I need an expression that tests for ten numbers & ignores everything else (??).<br>
<br>
I tried:<br>
if (!$FORM{'Phone'} =~ /\d{10,10}/) {<br>
edit_error(&quot;'Phone' is required\n&quot;);<br>
}<br>
<br>
thinking that \d is a digit and {10,10} requires a minimum & maximum of 10 digits. I didn't work.<br>
<br>
I'm just learning regular expressions, figured this would be a good example to work on. In reality I'll probably break the phone number into three fields & edit them for numerics. Still curious to know what the answer to the original question is though.
 
That's quite hard - I'm sorry I answered this now!<br>
<br>
Anyway - my Perl For Really Stupid People book is at home. I'll dig it out and post an answer (or something) this evening (GMT)<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Not extensively tested, but try this...<br>
<br>
#!/bin/perl<br>
$String = &quot;a1b2c3d4()%^&quot;; # A test string<br>
<br>
$String =~ s/(\W¦[a-zA-Z]¦_)//g; # &lt;--- The important bit :)<br>
<br>
print $String, &quot;\n&quot;; # The amended string.<br>
<br>
The regexp matches non-alphanumerics with the &quot;\W&quot;, alphas with the &quot;[a-zA-Z]&quot; bit, and &quot;_&quot;, because this is considered to be alphanumeric. It then removes any occurrence of these characters from the original string (using the &quot;=~&quot; operator). The pipe &quot;¦&quot; signs within the braces &quot;()&quot; are &quot;or&quot; operators for the regexp.<br>
<br>
You might want to expand the test a little to make sure it strips out all illegal data entry. One thing it doesn't do, for example, is strip out control characters. This is left as an exercise for the reader ;^)<br>
<br>
Hope this helps.
 
Thanks, I'll work with this & figure it out.
 
<br>
I see people bypass this most often by providing 3 fields on the form, something like &quot;( ___ ) ___ - ____&quot; that way no parsing is required. If you really want to just use one field you might try<br>
/(\d{3,3})\D*(\d{3,3})\D*(\d{4,4})/<br>
<br>
Have fun!<br>
Kai.<br>

 
Thanks,<br>
<br>
I'm using this as a learning excercise - I'll give it a try.<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top