Simple E-mail Validation in PHP using JavaScript(jQuery) |
X

Congrats, You are Subscribed to Receive Updates.

Simple E-mail Validation in PHP using JavaScript(jQuery)


Simple E-mail Validation in PHP using JavaScript(jQuery). generally we do validations by two ways.

  1. Client-side
  2. Server-side

php-jquery-kv

1. Client -Side :

We have to verify that the user entered data is an email. But we can do this by using JavaScript or  jQuery.  Here is the code to validate email. Here is the html first.

<form method="post">
   <input type="text" name="user_email" value=""  onChange="kv_validateEmail(this)">
</form>

And here is the js function to form the validation.

function kv_validateEmail(email) {
    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return re.test(email);
}

this is just client side validation. it wont check whether the same email exist in database or not.

2. Server -Side:

You can do server-side validation by using php, which simple and good to, when you need to perform new user registration. Because, you need to check the same email id used to register again . it will conflict, when you do the same email again and again .  So we need to check it with help of PHP.

<form method="post" action="receive_process.php"> // it will submit the data to the file receive_process.php...
    <label> name: <input type="text" name="user_name" value=""> </label><br>
    <label> Email : <input type="text" name="user_email" value=""> </label><br>
    <label> Password : <input type="password" name="password" value=""> </label><br>
<input type="submit" name="submit_values" value=" Submit" > <br>
</form>

Let’s write the PHP function for this form actions.  receive_process.php

<?php 
$kv_con = mysqli_connect("localhost", "root", "my_password", "kvc_db");
 if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
	$fields = array(
		'user_name',
		'user_email',
		'password'						
	);
	foreach ($fields as $field) { //Initial cleanup work to remove unwanted issues ,
		if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field])); else $posted[$field] = '';
	}
}

and now, we take an example table in database. the following query will help you to create a users table in your database.

CREATE TABLE IF NOT EXISTS `kvc_users` (
`ID` bigint(20) unsigned NOT NULL,
  `user_name` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',  
  `user_email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `password` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Than , you may enter a demo data into it.  Now , we need to proceed the function to validate the email on server side. Now moving to the same php file, which we processed already.

<?php 
$kv_con = mysqli_connect("localhost", "root", "my_password", "kvc_db");
 if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
	$fields = array(
		'user_name',
		'user_email',
		'password'						
	);
	foreach ($fields as $field) { //Initial cleanup work to remove unwanted issues ,
		if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field])); else $posted[$field] = '';
	}
if ($kv_con) {

if (filter_var($posted['user_email'], FILTER_VALIDATE_EMAIL)) {
	$found = mysqli_query($kv_con,"SELECT COUNT(*) FROM kvc_users WHERE 'user_email'=".$posted['user_email']);
	if(!$found) {
		// you can proceed save operations. 
               echo "true"
	} else {
		// Report the bug here...
               echo " already Exist"; 
	}
  }
  }
   mysqli_close($kv_con);
}
exit();

we have to show the results after getting response from server.  But its older method. Now a days, we expect sudden response it minutes. So we have to go with jQuery AJAX and  PHP.

var url = 'receive_progress.php';
var form_data = $('form').serialize();
$.post('url', form_data, success: function(result){
        $("#div1").html(result); // div1 is the id of the another div, which will notify you. 
    }); // Show html things. 

Now, the same validation done through serer-side. that’s it. Check it and write me your comments below.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar