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=
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("'Phone' is required\n"<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=
Not extensively tested, but try this...<br>
<br>
#!/bin/perl<br>
$String = "a1b2c3d4()%^"; # A test string<br>
<br>
$String =~ s/(\W¦[a-zA-Z]¦_)//g; # <--- The important bit <br>
<br>
print $String, "\n"; # The amended string.<br>
<br>
The regexp matches non-alphanumerics with the "\W", alphas with the "[a-zA-Z]" bit, and "_", because this is considered to be alphanumeric. It then removes any occurrence of these characters from the original string (using the "=~" operator). The pipe "¦" signs within the braces "()" are "or" 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.
<br>
I see people bypass this most often by providing 3 fields on the form, something like "( ___ ) ___ - ____" 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>
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.