jquery检查现有的电子邮件地址以进行表单验证

时间:2014-02-24 13:55:00

标签: php jquery jquery-validate

我有一个注册表单,我使用以下代码验证表单客户端,问题是我输入相同的密码,显示字段不匹配。我也在检查存在的电子邮件地址,这也是无法正常工作请找我的代码片段并告诉我在哪里做错了。

$('#UserSignupForm')
   .validate({
        rules : "data[User][firstname]" : "required",
                "data[User][lastname]" : "required",
                "data[User][email]" : {
                            required : true,
                            email : true,
                            remote : baseUrl + '/checkEmail'
                },  
                "data[User][password]" : {
                            required : true,
                            minlength : 5
                },
                "data[User][confirm_password]" : {
                            required : true,
                            minlength : 5,
                                            //equalTo : "#UserPassword"
                }
                },

                messages : {
                    "data[User][firstname]" : "firstname is missing*",
                    "data[User][lastname]" : "lastname is missing*",
                    "data[User][email]" : {
                        remote : "This email is already taken",
                        email : "Please enter a valid email address"
                    },
                    "data[User][password]" : {
                        required : "Please provide a password",
                        minlength : "missing*"
                    },
                    "data[User][confirm_password]" : {
                        required : "Please provide a password",
                        minlength : "Your password must be at least 5 characters long",
                    }
                },

检查我在控制器中执行以下代码的唯一用户名

public function checkEmail(){
   $this->autoRender = false;
   $this->loadModel('UsersEntity');
   $email = $this -> request->data['User']['email'];

   $sql = $this->User->query("Select  username FROM users_entity WHERE username = {$email}");
   if($sql){
       return true;
   }
}

2 个答案:

答案 0 :(得分:1)

你有一个语法错误......你错过了rules中的左括号。

$('#UserSignupForm').validate({
    rules : {                     // <-- opening brace was missing
        "data[User][firstname]" : "required",
        "data[User][lastname]" : "required",
        "data[User][email]" : {
            required : true,
            email : true,
            remote : baseUrl + '/checkEmail'
        },  
        "data[User][password]" : {
            required : true,
            minlength : 5
        },
        "data[User][confirm_password]" : {
            required : true,
            minlength : 5,
            //equalTo : "#UserPassword"
        }
    },
    messages : {
        "data[User][firstname]" : "firstname is missing*",
        "data[User][lastname]" : "lastname is missing*",
        "data[User][email]" : {
            remote : "This email is already taken",
            email : "Please enter a valid email address"
        },
        "data[User][password]" : {
            required : "Please provide a password",
            minlength : "missing*"
        },
        "data[User][confirm_password]" : {
            required : "Please provide a password",
            minlength : "Your password must be at least 5 characters long",
        }
    }  // <-- remove trailing comma
});    // <-- closing braces for '.validate()'

答案 1 :(得分:0)

您的SQL查询应该返回错误。因为您必须在$ email变量周围添加引号。

$sql = $this->User->query("Select  username FROM users_entity WHERE username = {$email}");
to 
$sql = $this->User->query("Select  username FROM users_entity WHERE username = '$email'");