简体   繁体   English

动态表单上的jQuery Custom Validate插件AddMethod

[英]Jquery Custom Validate Plugin AddMethod on Dynamic Forms

I am trying to use the Jquery Validate Plugin and I was able to figure out how to use it on static fields. 我正在尝试使用Jquery Validate插件,并且能够弄清楚如何在静态字段上使用它。

My problem is on how to used it on my dynamic forms. 我的问题是如何在动态表单上使用它。 I created a fiddle to discuss my problem. 我制造了一个小提琴来讨论我的问题。 Hope you can have patience on reading this. 希望您能耐心阅读。 The fiddle link is here 小提琴的链接在这里

Now, here's what I want to do, validate the Machine ID entered on each text box and make sure that it is unique. 现在,这就是我要做的,验证在每个文本框中输入的计算机ID,并确保它是唯一的。 Say you enter ABCD-123 twice, the system should alert you that it is not unique. 假设您两次输入ABCD-123,系统应提醒您它不是唯一的。

Hoping somebody could lend me a hand on this. 希望有人可以帮我这个忙。 Thanks 谢谢

first of all - put comments in your code while you write. 首先-在编写代码时在代码中添加注释。

the error is here: 错误在这里:

 var arrElements = $(".machineID");

arrElements is always empty, there is no element with machineID class arrElements总是空的,也没有元machineID

and your each doesn't work. 而且你们每个人都不起作用。 I refactored it http://jsfiddle.net/PaTJ4/ 我将其重构为http://jsfiddle.net/PaTJ4/

Your code could use some more fixes, it's a bit too complicated for that task. 您的代码可能需要使用更多修复程序,对于该任务而言有点太复杂了。 But now it works. 但是现在可以了。

good luck 祝好运

I've improved on your version - when a duplication is detected, any one of the duplicated fields should be marked as invalid, and should be able to be changed in order to fix the problem. 我对您的版本进行了改进-检测到重复项时,任何重复的字段都应标记为无效,并且应该可以对其进行更改以解决此问题。 So, you'd need to revalidate all other fields every time (using validator.element() ), but avoid a recursion (using validator.validatingOthers ). 因此,您每次都需要重新验证所有其他字段(使用validator.element() ),但要避免进行递归操作(使用validator.validatingOthers )。

I'll post the code for checkMachineIDs here for completeness: 为了完整性,我将在此处发布checkMachineID的代码:

    function checkMachineIDs(element){
        if($(element).val() != ""){
            var arrElements = $("#machineList .machineID"); 
            var $element = $(element);
            var validator = $($element[0].form).validate();
            if(arrElements.length > 1){
                var valid = true;
                arrElements.not('#'+$element.attr('id')).each(function() {
                  var current = $(this); 
                  if (current.val() == $element.val())
                    valid = false;
                });
                if (!validator.validatingOthers) {
                    validator.validatingOthers = true;
                    arrElements.not('#'+$element.attr('id')).each(function() {
                        validator.element(this);
                    });
                    if (valid) validator.element($element);
                    validator.validatingOthers = false;
                } 
                return valid;
            }else{
                return true;
            }
        }else{
            return true;
        }
    }

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

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