jQuery 带有 jQ​​uery 验证插件的自定义方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15589152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:23:00  来源:igfitidea点击:

Custom method with jQuery Validation plugin

jqueryjquery-validate

提问by Sharath Kumar

Im trying to work with custom validation in Jquery. All the coding part is right,but i am not getting where its going wrong...here is the part of code.

我试图在 Jquery 中使用自定义验证。所有的编码部分都是正确的,但我不明白哪里出了问题......这是代码的一部分。

Password:<input type="password" id="pnameTxt" name="pnameTxt" placeholder="Enter Password" size=12 class='required'><br>
Confirm Password:<input type="password" id="pnameTxt2" name="pnameTxt2" placeholder="Retype Password" size=15 class='required passwordCheck'><br>

Custom validation method:

自定义验证方法:

 $.validator.addMethod("passwordCheck",function (value,element){
          return value==$("#pnameTxt").val(); 

        }, 'Password and Confirm Password should be same');

回答by Sparky

Your code is working. You also have to assign the rule to your field when you initialize the plugin with .validate().

您的代码正在运行。当您使用.validate().

Working DEMO: http://jsfiddle.net/KrLkF/

工作演示:http: //jsfiddle.net/KrLkF/

$(document).ready(function () {

    $.validator.addMethod("passwordCheck", function (value, element) {
        return value == $("#pnameTxt").val();
    }, 'Password and Confirm Password should be same');

    $('#myform').validate({ // initialize the plugin
        rules: {
            pnameTxt2: {
                passwordCheck: true
            }
        }
    });

});


HOWEVER, you do not need to write a custom method for this functionality. The jQuery Validate plugin already has an equalTorule, and here's how to use it.

但是,您不需要为此功能编写自定义方法。jQuery Validate 插件已经有了一个equalTo规则,这里是如何使用它。

Working DEMO: http://jsfiddle.net/tdhHt/

工作演示:http: //jsfiddle.net/tdhHt/

$(document).ready(function () {

    $('#myform').validate({ // initialize the plugin
        rules: {
            pnameTxt2: {
                equalTo: "#pnameTxt" // using `id` of the other field
            }
        },
        messages: {
            pnameTxt2: {
                equalTo: "Password and Confirm Password should be same"
            }
        }
    });

});

回答by Marcel Gwerder

Have u initialized the validation plugin correctly? When I put the html in a formand then initialize the pluginit works as expected.

您是否正确初始化了验证插件?当我将 html 放入 aform然后initialize the plugin它按预期工作时。

<form id="test">
Password:<input type="password" id="pnameTxt" name="pnameTxt" placeholder="Enter Password" size=12 class='required'><br>
Confirm Password:<input type="password" id="pnameTxt2" name="pnameTxt2" placeholder="Retype Password" size=15 class='required passwordCheck'><br>
<input type="submit">
</form>
$.validator.addMethod("passwordCheck",function (value,element){
      return value==$("#pnameTxt").val(); 

    }, 'Password and Confirm Password should be same');

$('#test').validate();