Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Checking Phone Numbers

0 views
Skip to first unread message

Kevin Murphy

unread,
Jun 14, 2007, 11:38:06 AM6/14/07
to php
I collect phone numbers via a web form that breaks the phone number
up into 3 parts (3-digit area, 3-digit prefix, etc). Occasionally, I
am having people put in a phone number such as 999-999-9999 or
000-000-0000 or other numbers that are all the same. How would I go
about comparing those three fields to see if they are all the same
numbers. I can't just exclude it anytime that the numbers all match,
for example 444 is a valid area code. It should only fail if all the
numbers are exactly the same.

All the processes I've come up with seem to be rather convoluted. Is
there a simple way to test for this?

--
Kevin Murphy
Webmaster: Information and Marketing Services
Western Nevada College
www.wnc.edu
775-445-3326


Robert Cummings

unread,
Jun 14, 2007, 11:45:20 AM6/14/07
to Kevin Murphy, php
On Thu, 2007-06-14 at 08:38 -0700, Kevin Murphy wrote:
> I collect phone numbers via a web form that breaks the phone number
> up into 3 parts (3-digit area, 3-digit prefix, etc). Occasionally, I
> am having people put in a phone number such as 999-999-9999 or
> 000-000-0000 or other numbers that are all the same. How would I go
> about comparing those three fields to see if they are all the same
> numbers. I can't just exclude it anytime that the numbers all match,
> for example 444 is a valid area code. It should only fail if all the
> numbers are exactly the same.
>
> All the processes I've come up with seem to be rather convoluted. Is
> there a simple way to test for this?

<?php

$number = $field1.$field2.$field3;
$digit = (int)substr( $number, 0, 1 );

if( ereg( '^'.$digit.'+$', $number ) )
{
echo 'All the same :/';
}

?>

Cheers,
Rob.
--
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'

Edward Kay

unread,
Jun 14, 2007, 11:56:37 AM6/14/07
to php

Whilst there are several ways to do this, it's not going to make someone
entering dummy data give you their real phone number. They'll just enter
another one that bypasses your check, e.g. 111-222-3333 or 321-321-4321.

I'd also caution about forcing any specific format on telephone numbers. I'm
in the UK and our phone numbers are in the format xxxx-xxx-xxxx,
xxx-xxxx-xxxx or xxxxx-xxxxxx. This would not work with your form. If people
are happy to give you their number, let them type it how it is. It could
even include other characters, e.g. +44 (0)1234 567890 ex 324

Edward

Daniel Brown

unread,
Jun 14, 2007, 1:25:31 PM6/14/07
to Edward Kay, php
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

My suggestion is that we require the United Kingdom and other
countries around the world to conform to our standards of telephone
numbers. After all, isn't that what we do in America; change the
world to meet our expectations or choose to shun them for not doing
so?

Nevermind.... that would go far beyond list topics and waaaay too
far into the realm of political dissent.

--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107

Kevin Murphy

unread,
Jun 14, 2007, 1:42:10 PM6/14/07
to php
Well, in my context where this is an application to apply to a
community college, I think I can safely exclude non-us phone
numbers. :-)

The code that Robert supplied seems to work just great. Thanks.

--
Kevin Murphy
Webmaster: Information and Marketing Services
Western Nevada College
www.wnc.edu
775-445-3326

Eric Butera

unread,
Jun 14, 2007, 1:43:21 PM6/14/07
to php
Like others have said, there isn't much you can do about this. People
are either going to give you their information or not. The only thing
you can do is make sure there is a value that could possibly be a
phone number for people who might have just forgot to input one.
Otherwise you're going to just end up stepping on someone's toes who
really means to do what they are doing.

If you really need to validate the phone number then put a notice up
saying US phone numbers only and give a very specific format that they
must adhere to. This isn't really a good thing though seeing as the
web is a global playground, but maybe you have your reasons.

Now this mindset doesn't transition to other values. If you are
expecting someones age and have the field Age (eg. 35) then you can
check that with ctype_digit() and redisplay the form to them if the
value does not pass.

Another example is that on some forms I make email addresses are
required so we can send them their order confirmation. I use a simple
regex with a hostname lookup to see if the domain exists. Even with
all that the user can always type exa...@example.com and it would
pass perfectly.

Just as an aside, validate data. Don't try to sanitize into something
you want. What I mean is if you are asking for a credit card number
don't do a preg_replace to strip out non-numbers. Instead do a
preg_match() or ctype_digit() on numbers only and if it doesn't match
exactly what you want, spit it back to the user and tell them to fix
it.

0 new messages