PHP Email and mobile number validation script - Mostlikers

06 February, 2014

PHP Email and mobile number validation script

In this post, i have explained about how to validate the email address and mobile number in server side. Usually, we use form validation scripts. It will work only client side,  But we need to check the form values in server side.



View Demo                Download

Generally, form validation we will check mostly email, mobile number, mandatory fields. Here I have used regular expression function to check whether valid email or mobile number.  

PHP 5 validate Email

Php 5 filter_var() it's a strong validation. It's easy to check the email id valid format or not.
$email = 'some email id';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid email format"; 
}


PHP - Regular Expressions

Regular expressions are nothing more than a sequence or pattern of characters, It provides the foundation of for matching a pattern.

This function searches a string for the pattern, returning true if the pattern exists, and false otherwise. Here I have used preg_match regular expression function. It will check post value string valid or not. 

Email valid pattern
$emailval = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';


Mobile valid pattern
$mob="/^[1-9][0-9]*$/"; 


PHP

Preg_match function will check valid string data.
<?php
if(isset($_POST['form_submit'])) {   
extract($_POST);
$emailval = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';
$mob="/^[1-9][0-9]*$/"; 
if($name=="" && $email=="" && $phone=="") :
    $msg="Please fill all mandatory fields";
elseif(!preg_match($emailval, $email)) :
    $msg="Please enter a valid email";
elseif(!preg_match($mob, $phone)) :
    $msg="Please enter a valid number";
else :
    // Write your insert coding
    $msg_sucess="Record inserted successfully";
    $msg="";
endif;
}
   ?>  

HTML

$msg - Validation error msg
<form name="form" method="post">
<label>Name</label>
<input type="text" name="name" /><br />
<label>Email &nbsp;</label>
<input type="text" name="email" /><br />
<label>Phone</label>
<input type="text" name="phone" /><br />
<input type="submit" name="submit" value="submit" />
<div class="<?=(@$msg_sucess=="") ? 'error' : 'green' ; ?>">
 <?php echo @$msg; ?><?php echo @$msg_sucess; ?></div>
</form>

CSS

<style type="text/css">
.error {
margin-top: 6px;
margin-bottom: 0;
color: #fff;
background-color: #D65C4F;
display: table;
padding: 5px 8px;
font-size: 11px;
font-weight: 600;
line-height: 14px;
}
.green{
margin-top: 6px;
margin-bottom: 0;
color: #fff;
background-color: green;
display: table;
padding: 5px 8px;
font-size: 11px;
font-weight: 600;
line-height: 14px;
}
</style>

Thank you for visiting. if have any queries write your comments below.

"God, our Creator, has stored within our minds and personalities, great potential strength and ability. Prayer helps us tap and develop these powers."
                                             
                                         - A. P. J. Abdul Kalam








Related Topics


3 comments:

  1. how to do 10 digit mobile validation using java script in php. please help me.

    ReplyDelete
    Replies
    1. if (phone.value.length != 10) {

      }
      else {
      alert("enter 10 digit");
      }

      Delete