Page 1 of 1

Mobile phone number validation

Posted: Mon Aug 09, 2010 7:10 pm
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

Re: Mobile phone number validation

Posted: Mon Aug 09, 2010 8:26 pm
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.

Re: Mobile phone number validation

Posted: Mon Aug 09, 2010 9:23 pm
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.

Re: Mobile phone number validation

Posted: Tue Aug 10, 2010 4:15 am
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