18

I am using the jquery validation and i need to validate an email.

I use

    $("#myForm").validate({
        rules: 
            email: {
                required: true,
                email: true
            }
})

SO FAR so good. The problem is that i need to make an ajax call to verify if given email already exist.If exist display message "This email already exits.Please selecte other".

Can anyone help me implement this.

skaffman
  • 398,947
  • 96
  • 818
  • 769

4 Answers4

18
remote: "/some/remote/path"

That path will be passed the value of the field in a $_GET. so.. what actually gets called in your case would be:

/some/remote/path?email=someemailuriencoded

Have the server side code return just the text true or false.

Then the corresponding message also named remote.

remote: "The corresponding email already exists"

My code for something similar:

$("#password_reset").validate({
  rules: { 
    email: { required: true, email: true, minlength: 6, remote: "/ajax/password/check_email" }
  }, 
  messages: { 
    email: { required: "Please enter a valid email address", minlength: "Please enter a valid email address", email: "Please enter a valid email address", remote: "This email is not registered" }
  }, 
  onkeyup: false,
  onblur: true
});

The corresponding server side code in php:

$email_exists = $db->prows('SELECT user_id FROM users WHERE email = ? LIMIT 1', 's' , $_GET['email'] );
if ( $email_exists ) { echo 'true'; } else { echo 'false'; }
exit;

Of course that's using my database abstraction stuff, but you get it.

Daren Schwenke
  • 5,428
  • 3
  • 29
  • 34
8

Well, this works for me...

 $('[id$=txtEmail]').rules("add", { required: true, email: true,
         remote:function(){
              var checkit={
                  type: "POST",
                  url:  WebServicePathComplete+"VerifyEmail",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  data: "{'email':'" +$('[id$=txtEmail]').val() + "'}"
              };
              return checkit;
         }
  });

note that I have a input with the id 'txtMail'

Rbacarin
  • 707
  • 6
  • 12
0

Whats your server language? PHP or ASP?

This is the jQuery part:

$.ajax({
    type: "POST",
    url: "YourWebserviceOrWhatEver",
    data: "{'Email':'your@ema.il'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      if(msg.EmailExists){
        //Email exists
        alert("This email already exists. Please select other");
      }
      else {
       //Email doesn't exist yet
       doSomething();
      }
    }
});
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
k0ni
  • 2,417
  • 4
  • 22
  • 25
0

first of all i want to tell you that if you are using post method in ajax then dont pass email in url.

 $.ajax({
        type: "POST",
        url: URLROOT + '/register/check/';
        data: {email:email};
         success: function (result) {
            if (result == 'exist') {
              return false;
             } else {
             return true;
             }
          }
      });`

after that you remove parameter from controller's function.

`public function check(l) {
  if($this->userModel->findUserByEmail($_POST['email'])==true)) {
   return 'exist';
  }
  return 'false';
 }`

Try with this.

Madhuri Patel
  • 1,270
  • 12
  • 24