Mobile phone number validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
J_Mo
Forum Newbie
Posts: 10
Joined: Mon Aug 09, 2010 7:02 pm

Mobile phone number validation

Post by J_Mo »

Hi,

I am new to PHP and am trying to validate a mobile phone number in an form I am working on. I have used a validation class that I found but am having trouble adding my own.

How do I say "is not equals to '07' followed by any other numbers"

I have this:

Code: Select all

 function DoValidate(&$formars,&$error_hash)
    {
        if($formars['Mphone']!="07"+"^[0-9]")
        {
            $error_hash['Mphone']="Please enter a valid mobile number";
            return false;
        }
    return true;
    }
Thanks
Phil
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: Mobile phone number validation

Post by MindOverBody »

Code: Select all

 
        if($formars['Mphone']!="07"+"^[0-9]")
        {...}
I think problem is in this line of code upthere. Your array item is being compared with sum of "07" and any other number given. You might try replace "+" with "."
That will join that two strings into one string while parsing.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Mobile phone number validation

Post by shawngoldw »

You need to look up regular expressions.
Try this one, I'm new to them so if it doesn't work...

Code: Select all

$regex = "!^07\d+$!";
if(!preg_match($regex, $formars['Mphone']))
{
// not a match
}
This will hopefully check that the string starts with 07, has 1 or more digits after it, and is only comprised of digits.
J_Mo
Forum Newbie
Posts: 10
Joined: Mon Aug 09, 2010 7:02 pm

Re: Mobile phone number validation

Post by J_Mo »

Thanks for you replies. I used

Code: Select all

$regex = "!^07\d+$!";
if(!preg_match($regex, $formars['Mphone']))
and it has worked perfectly.

Cheers
Phil
Post Reply