带有jQuery Validation插件的自定义方法 [英] Custom method with jQuery Validation plugin

查看:224
本文介绍了带有jQuery Validation插件的自定义方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Jquery中使用自定义验证.所有的编码部分都是正确的,但是我没有弄错它在哪里...这是代码的一部分.

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.

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>

自定义验证方法:

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

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

推荐答案

您的代码正在运行.使用.validate()初始化插件时,还必须将规则分配给您的字段.

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

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

Working DEMO: 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
            }
        }
    });

});


如何,您无需为此功能编写自定义方法. jQuery Validate插件已经具有 equalTo规则,下面是使用方法.


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

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

Working DEMO: 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"
            }
        }
    });

});

这篇关于带有jQuery Validation插件的自定义方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆