Have an account? Sign in
Login  Register  Facebook
How to validate email and check for duplicate record
Hello,

I want to check for a duplicate email in the database but cannot validate the user input.

I want to check if the user input is a valid email or not, if it is not an error message is to be displayed.

my JS code is:


//When the dom is ready 
$(document).ready(function() {

//if theres a change in the email textbox
$("#email").change(function() { 
//Get the value in the email textbox

var email = $("#email").val();
var length = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
//if the lenght greater than 3 characters
if(email.length > 3) { 

//Add a loading image in the span id="availability_status"
$("#email_status").html('<img src="images/loader.gif" align="absmiddle">&nbsp;Checking availability...');

//Make the Ajax Request
$.ajax({  
    type: "POST",  
	//file name
    url: "../livecheck.php", 
	//data
    data: "email="+ email,  
    success: function(server_response){  
   
   $("#email_status").ajaxComplete(function(event, request){ 

	//if ajax_check_email.php return value "0"
	if(server_response == '0') { 
	//add this image to the span with id "availability_status"
	$("#email_status").html('<img src="images/available.png" align="absmiddle"> <font color="Green"> Available </font>');
	
	//if it returns "1"
	} else if(server_response == '1') {  
	 $("#email_status").html('<img src="images/not_available.png" align="absmiddle"> <font color="Red">Not Available </font>');
	}  
   
   });
   } 
   
  }); 

} else {
//if in case the email is less than or equal 3 characters only 
$("#email_status").html('<font color="#cc0000">Invlaid email, please correct</font>');

}

return false;
});

});

My php code to check for email in database is:

<?php
require_once("includes/database.php"); 
require_once("includes/functions.php");
//Include The Database Connection File 

if($_POST && !empty($_POST['email'])) { //If a email has been submitted 

$email = mysql_real_escape_string($_POST['email']);//Some clean up :)

	$email = $_POST['email'];
	
	if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

	$check_for_username = mysql_query("SELECT id FROM users WHERE email = '$email'");
	//Query to check if email is available or not 

if(mysql_num_rows($check_for_username)) {
	echo '1'; //If there is a  record match in the Database - Not Available
	} else {
	echo '0'; //No Record Found - Email is available 
		}
	}
}

?>



Can anyone help to correct this please.

thank you
Started: December 11, 2011 Latest Activity: June 1, 2018 PHP JS PHPMySQL
3 Answers
Buy Adderall In The Uk cialis generic Canada Pharmacy On Line

Posted: Ellgaibra
In: June 1, 2018

This still does not validate email address to see if it is a valid email being provided

Posted: qakbar
In: December 11, 2011

Your if statement will return true in most casesTry to replace it to
if(mysql_num_rows($check_for_username) > 0) {
    echo '1'; //If there is a  record match in the Database - Not Available
    } else {
    echo '0'; //No Record Found - Email is available
        }
    

Posted: hemagx
In: December 11, 2011

Edited: hemagx
In: December 11, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer