JQuery Validator Plugin - checking existing username and email in mysql database

I have successfully created a form that represents and adds users to the mysql database, and the jQuery Validator plugin validation form works fine for everything except checking if the username exists in the database ...

I just spent about 8 hours reading and trying to figure out a way to define a new method using the jQuery Validator plugin. I just can't figure out how I will check the database for the entered username or email and return whether it exists or not using jQuery.

My code is:

<script src="../assets/js/main.js"></script> <script src="../assets/js/ajax.js"></script> <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.min.js"></script> <!-- FORM VALIDATION --> <script type="text/javascript"> jQuery.validator.addMethod("checkExists", function(value, element) { //No idea what to call here }, "Username already exists." ); //<![CDATA[ $(window).load(function(){ $("form").validate({ rules: { username: {minlength: 3, required: true, checkExists: true}, email: {email: true, required: true}, pass1: {minlength: 3, required: true}, pass2: {minlength: 3, required: true, equalTo: "#pass1"}, country: {required: true}, tandc: {required: true}, }, messages: { username: {required: "You need to enter a Username."}, email: {required: "You need to enter an Email Address."}, pass1: {required: "You need to enter a Password."}, pass2: {required: "You need to enter your password again.", equalTo: "Your passwords don't match."}, country: {required: "You need to tell us where you live."}, tandc: {required: "You need to read and agree to the Terms and Conditions to use CGE."} }, showErrors: function(errorMap, errorList) { $.each(this.successList, function(index, value) { return $(value).popover("hide"); }); return $.each(errorList, function(index, value) { var _popover; console.log(value.message); _popover = $(value.element).popover({ trigger: "manual", placement: "right", content: value.message, template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-inner\"><div class=\"popover-content\"><p></p></div></div></div>" }); _popover.data("popover").options.content = value.message; return $(value.element).popover("show"); }); } }); });//]]> </script> 

If someone smart might change my code to show me how I should have done it, that would be a big help - I feel like I'm going to go crazy!

Thanks in advance, I can’t wait for a solution :-)


EDIT is my current code

Nothing similar happens, it seems to me that I'm closer:

CURRENT CODE:

signup.php

  $(window).load(function(){ $("form").validate({ rules: { username: {minlength: 3, required: true}, email: {email: true, required: true, remote: {url: "./validation/checkUnameEmail.php", type : "post"}}, pass1: {minlength: 3, required: true}, pass2: {minlength: 3, required: true, equalTo: "#pass1"}, country: {required: true}, tandc: {required: true} }, 

checkUnameEmail.php

 <?php include_once(".../php_includes/db_conx.php"); $email = urldecode($_POST['email']); $result = mysqli_query($db_conx, "SELECT * FROM users WHERE email = '$email' LIMIT 1;"); $num = mysqli_num_rows($result); if($num == 0){ echo "true"; } else { echo "E-Mail-Adresse schon registriert."; } mysqli_close($db_conx); ?> 

* db_conx.php *

 <?php $db_conx = mysqli_connect("localhost", "root", "root", "membership"); //Evlauate the connection if (mysqli_connect_errno()){ echo mysqli_connect_error(); exit(); } ?> 
+7
javascript jquery php mysql validation
source share
2 answers
 $.validator.addMethod("checkExists", function(value, element) { var inputElem = $('#register-form :input[name="email"]'), data = { "emails" : inputElem.val() }, eReport = ''; //error report $.ajax( { type: "POST", url: validateEmail.php, dataType: "json", data: data, success: function(returnData) { if (returnData!== 'true') { return '<p>This email address is already registered.</p>'; } else { return true; } }, error: function(xhr, textStatus, errorThrown) { alert('ajax loading error... ... '+url + query); return false; } }); }, ''); 

OR

Instead, you can use a remote method that allows you to perform remote checks: http://docs.jquery.com/Plugins/Validation/Methods/remote

Eg.

  $("#yourFormId").validate({ rules: { email: { required: true, email: true, remote: { url: "checkUnameEmail.php", type: "post" } } }, messages: { email: { required: "Please Enter Email!", email: "This is not a valid email!", remote: "Email already in use!" } } }); 

checkUnameEmail.php // For example.

  <?php $registeredEmail = array(' jenson1@jenson.in ', ' jenson2@jenson.in ', ' jenson3@jenson.in ', ' jenson4@jenson.in ', ' jenson5@jenson.in '); $requestedEmail = $_REQUEST['email']; if( in_array($requestedEmail, $registeredEmail) ){ echo 'false'; } else{ echo 'true'; } ?> 
+14
source share

js code

 username: { required: true, minlength: 5, remote: '/userExists' }, 

Php code to check for the presence and return of messages

 public function userExists() { $user = User::all()->lists('username'); if (in_array(Input::get('username'), $user)) { return Response::json(Input::get('username').' is already taken'); } else { return Response::json(Input::get('username').' Username is available'); } } 
+1
source share

All Articles