PDA

View Full Version : Mobile phone number validation


J_mo
08-09-2010, 11:40 PM
Hi,

I am trying to validate a mobile phone number in an form I am working on.

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

I have this:

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

edbr
08-10-2010, 02:34 AM
you could break the number and select the first 2 , then a simpler if condition based just on that not being 07

$var = '071020000';
list($var1, $var2) = explode('|', substr_replace($var, '|', 2, 0));

echo $var1;

davidj
08-10-2010, 10:13 AM
I use Regex for matching numbers and strings

I think its better


<?php

$number = "073450723";

// note the * which will match any number of numbers after the 07.
// you can replace * with {7} including the braces which will match 7 numbers after the 07 if you want to test for length

if(preg_match('/^[0][7]+[0-9]*/', $number)){

echo "Found a match"; // apply your own functionality here

}else{

echo "No match found"; // apply your own functionality here
}

?>

J_mo
09-02-2010, 04:44 PM
Thanks both.

I used david j's version. Spot on.

Phil