jquery validation with ajax call

前端 未结 4 1285
闹比i
闹比i 2020-12-06 00:58

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

I use

    $(\"#myForm\").validate({
        rules: 
            email: {
                   


        
4条回答
  •  清歌不尽
    2020-12-06 01:23

    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.

提交回复
热议问题