/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Phone number regex?

I’ve found a couple out there – but not to the extent in which I want..

I want to be able to match:
1-(817)-555-1212
(817)-555-1212
1-817-555-1212
[817] 555 1212
817x555x1212
817.555.1212
…. and the rest of the mix and match of -‘s,.’s, [‘s, (‘s, x’s, etc..

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@LeeUAug 08.2007 — Why not just use a mask?
Copy linkTweet thisAlerts:
@ellisglauthorAug 08.2007 — Not everyone is going to have javascript turned on. So validating the phone number on the server side has to be done. Even if everyone in the world had it turned on - I still would do it server side to make absolutely sure the input is clean before passing it to the DB.
Copy linkTweet thisAlerts:
@LeeUAug 08.2007 — Gotcha! How about something here?
Copy linkTweet thisAlerts:
@ellisglauthorAug 08.2007 — Right now I'm using:

$regex = "/^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+.[a-z]{2,}$/i";

I'll have to look at that link a bit more in depth. I've seen some that had optional extensions. I'm just not all that great with regex stuff.

I think mine will do for the time being - but trying to eleminate the pbkac issues as much as possible.
Copy linkTweet thisAlerts:
@ellisglauthorAug 08.2007 — er: that was for email: this is one i'm using:

$regex = "/^(?:([2-9]d{2}) ?|[2-9]d{2}(?:-?| ?))[2-9]d{2}[- ]?d{4}$/";
Copy linkTweet thisAlerts:
@bokehAug 08.2007 — Doh!!

Here's my two cents. The only thing important about a telephone number is the digits so just remove the rest. Once you have just the digits it is so much easier to decide if the number is valid or not.

[code=php]$OnlyDigits = preg_replace('/[^d]/', '', $input);[/code]
Copy linkTweet thisAlerts:
@ellisglauthorAug 09.2007 — Ah - a lot easier than I can deal with it with basic logic.
×

Success!

Help @ellisgl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...