jQuery验证等于不与Grails一起使用

时间:2016-09-17 16:43:57

标签: jquery grails jquery-validate

我在Grails应用程序中使用jQuery Validate plugin, 以下是GSP中用于验证的代码:

 <script type="text/javascript">

    $(document).ready(function() {
        $('.save').attr('disabled', true);
        jQuery.validator.addMethod("noSpace", function(value, element,e) { 

        return value.indexOf(" ") < 0 && value != ""; 
          }, "${message(code:'sherif.jqueryValidation.nospaces')}");


    $('#myForm').validate({

        errorElement: "label",
        errorClass:'error',
        success: function() {
         $('.save').attr('disabled', false);
         },
        rules :
            {
            username:{required : true,noSpace:true},
            password :{required : true,noSpace:true},
            confirmPassword:{required : true,noSpace:true,equalTo:"#password"},
            contactNumber: { required : true, number:true},
            email:{required:true , email:true}
            }
    ,
        messages : {
            username:{required :"${message(code:'sherif.jqueryValidation.required')}"},
            password : {required :"${message(code:'sherif.jqueryValidation.required')}"},
            email:{required :"${message(code:'sherif.jqueryValidation.required')}" , email:"${message(code:'sherif.jqueryValidation.invalidemail')}"},
            confirmPassword: {required :"${message(code:'sherif.jqueryValidation.required')}" , equalTo:"${message(code:'sherif.jqueryValidation.passwordmatch')}"},
            contactNumber: { required : "${message(code:'sherif.jqueryValidation.required')}", number:"${message(code:'sherif.jqueryValidation.numberonly')}"}
    }

    });
});
</script> 

equalTo验证外,所有验证工作正常,但两个密码字段的值相同,我得到password doesn't match错误。

以下是密码字段:

 <div class="row form-group">

    <div class="fieldcontain ${hasErrors(bean: usersInstance, field: 'password', 'error')} required">
    <div class="col-sm-2 "><label for="password">
        <g:message code="users.password.label" default="Password" />
        <span class="required-indicator">*</span>
    </label>
    </div>
    <div class="col-sm-10"> <g:passwordField style = "width:40%;"  class="form-control input" name="password" required="" value="${usersInstance?.password}"/>
    </div>
    </div>
</div>

<div class="row form-group">
<div class="fieldcontain ${hasErrors(bean: usersInstance, field: 'confirmPassword', 'error')} required">
    <div class="col-sm-3 "><label for="confirmPassword">
        <g:message code="users.confirmPassword.label" default="confirm Password" />
        <span class="required-indicator">*</span>
    </label>
    </div>
<div class="col-sm-9">  <g:passwordField style = "width:40%;"  class="form-control input" name="confirmPassword" required="" value="${usersInstance?.confirmPassword}"/>
</div>
</div>
</div>

我有什么遗漏可能会导致此错误吗?

1 个答案:

答案 0 :(得分:3)

  

...虽然两个密码字段的值相同但我得到"password doesn't match"错误。

如果没有符合#password的元素,您将看到这种情况。

equalTo: "#password"

您的#password选择器正在查找包含id="password"的元素。如果您无法为此元素添加id属性,则可以尝试以下内容,该内容将匹配name ...

equalTo: "[name='password']"

DEMO:jsfiddle.net/68a9fkgt/