Mobile number & EMail validate in Php

With Php its too easy to validate user entered data using regular expression. here very simple and most required EMail and Mobile number validation Php script with regular expression

Email and Mobile Number Validation using regular expression



$phone="9876738891";
$email= "myemail@domain.com";

$emailval = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';
$mob="/^[789][0-9]{9}$/";

if(!preg_match($emailval, $email))
{ 
    $msg="Email not valid";
}
else
{   
if(!preg_match($mob, $phone))
{ 
    $msg="Mobile number not valid";
        
}   
else
{       
   // Additional validation / mysql operation comes here        
}
}

To allow user enter only number in textbox instead of validate entered number string length you can also use below syntax

if(preg_match('/^([0-9]*)$/', $phone))
   $msg = "Valid";

Post a Comment

0 Comments