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.
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 </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.
- A. P. J. Abdul Kalam
Related Topics
- Generate RSS Feed for website using PHP and JSON script
- Ajax add,view and delete using MySQL
- Active and inactive users concept using PHP and Ajax
- How to Make a Simple Search Engine using PHP and MySQLi
- PDO basic insert,view,update,delete with PHP function
- Send email with HTML template using PHP
- Login with facebook using CodeIgniter
how to do 10 digit mobile validation using java script in php. please help me.
ReplyDeleteif (phone.value.length != 10) {
Delete}
else {
alert("enter 10 digit");
}
cxzxcxzxc
ReplyDelete