简体   繁体   English

jQuery验证equalTo不能与Grails一起使用

[英]jQuery Validation equalTo not working with Grails

I'm using jQuery Validate plugin in my Grails application , Here is the code from the GSP for the validation: 我在Grails应用程序中使用jQuery Validate插件 ,这是来自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> 

All the validations are working fine except equalTo validation, although the two password fields have the same value I get a password doesn't match error. 除了equalTo验证以外,所有验证都工作正常,尽管两个密码字段具有相同的值,但我得到的password doesn't match错误。

Here are the Password fields : 以下是密码字段:

 <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>

Is there anything I missed that may cause this error? 我错过了任何可能导致此错误的内容吗?

...although the two password fields have the same value I get a "password doesn't match" error. ...尽管两个密码字段的值相同,但出现"password doesn't match"错误。

You will see this condition if there is no element matching #password . 如果没有与#password匹配的元素,您将看到这种情况。

equalTo: "#password"

Your #password selector is looking for an element that contains id="password" . 您的#password选择器正在寻找包含id="password"的元素。 If you cannot add an id attribute to this element, you can try the following, which will match by name ... 如果您不能向该元素添加id属性,则可以尝试以下操作,这些操作将按name匹配...

equalTo: "[name='password']"

DEMO: jsfiddle.net/68a9fkgt/ 演示: jsfiddle.net/68a9fkgt/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM