×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Regular expression for phone number?

Regular expression for phone number?

Regular expression for phone number?

(OP)
Does anyone have a regular expression for editing a phone number?

Thanks in advance!

RE: Regular expression for phone number?

(OP)
I have an input form that includes a phone number field. I want to edit it for validity. Phone numbers could be entered as:
(203) 555-1212
or
203.555.1212
etc.

I guess I need an expression that tests for ten numbers & ignores everything else (??).

I tried:
if (!$FORM{'Phone'} =~ /\d{10,10}/) {
edit_error("'Phone' is required\n");
}

thinking that \d is a digit and {10,10} requires a minimum & maximum of 10 digits. I didn't work.

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.

RE: Regular expression for phone number?

That's quite hard - I'm sorry I answered this now!

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)

Mike

Mike Lacey
Mike_Lacey@Cargill.Com
Cargill's Corporate Web Site

RE: Regular expression for phone number?

Not extensively tested, but try this...

#!/bin/perl
$String = "a1b2c3d4()%^"; # A test string

$String =~ s/(\W¦[a-zA-Z]¦_)//g; # <--- The important bit :)

print $String, "\n"; # The amended string.

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.

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 ;^)

Hope this helps.

RE: Regular expression for phone number?

(OP)
Thanks, I'll work with this & figure it out.

RE: Regular expression for phone number?


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
/(\d{3,3})\D*(\d{3,3})\D*(\d{4,4})/

Have fun!
Kai.

RE: Regular expression for phone number?

(OP)
Thanks,

I'm using this as a learning excercise - I'll give it a try.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close