简体   繁体   English

Validate.js自定义方法

[英]Validate.js custom method

in the form i'm creating javascript generates dinamically a certain number of divs (depending on user s choice) which are containing text inputs. 在我创建javascript的形式中,javascript会动态生成一定数量的div(取决于用户的选择),其中包含文本输入。 Javascript is also genarating class and names for the inputs as follows: JavaScript也为输入提供了类和名称,如下所示:

DIV1 第1版

<input class="pax_from0" type="text" name="pax_from0" aria-required="true">
<input class="pax_to0" type="text" name="pax_to0" aria-required="true">

DIV2 第2版

<input class="pax_from1" type="text" name="pax_from1" aria-required="true">
<input class="pax_to1" type="text" name="pax_to1" aria-required="true">

DIV3 第3版

<input class="pax_from2" type="text" name="pax_from2" aria-required="true">
<input class="pax_to2" type="text" name="pax_to2" aria-required="true">

and so on... 等等...

Now i' m trying to validate the form with validate.js so that ...for example...pax_from1 MUST have higher value then pax_to0. 现在,我正在尝试使用validate.js验证表单,以便...例如... pax_from1必须比pax_to0具有更高的值。

I 've made a draw to outline the concept better 我画了一个更好的概图

在此处输入图片说明

This is the code I wrote so far... 这是我到目前为止编写的代码...

new.js new.js

(This file creates dinamically the elements within a for loop ...here i'm adding the roles to the elements i ve just created) (此文件动态创建for循环内的元素...在这里,我将角色添加到刚刚创建的元素中)

$("input[name=pax_to"+i+"]").rules("add", {
        required: true,
        digits:true
});
$("input[name=pax_from"+i+"]").rules("add", {
        required: true,
        digits:true
});
if($("#element1").length>0){   //at least 1 div exists
   $("input[name=pax_from"+i+"]").rules("add", {
      checkPrevValuePaxTo : true
    });
}

validate-new.js validate-new.js

(This is the javascript file that validates my form, the custom method i've created is checkPrevValuePaxTo ) (这是验证我的表单的javascript文件,我创建的自定义方法是checkPrevValuePaxTo

$(function () {

$('#create-new-route').validate({

    rules: {
        origine: {
            required: true
        },
        destinazione: {
            required: true
        },
        pippi: {
            required: function () {
                return $('#element0').length > 0;
            }
        }
    },

    messages: {
        origine: {
            required: "Inserire la località di partenza"
        },
        destinazione: {
            required: "Inserire la località di destinazione"
        },
        fasce_select: {
            required: "Inserire almeno una fascia di prezzo"
        }
    },

    submitHandler: function () {
        if ($('#element0').length <= 0) {
            alert("E' necessario creare almeno una fascia di prezzo");
            return false;
        }
        saveForm(json);
        return false;
    }
});

jQuery.validator.addMethod("checkPrevValuePaxTo", function (value, element) {

    var el = element.className;
    var temp = el.match(/\d+/g).map(Number);
    var i = temp - 1;

    var elPrevious = $("input[name=pax_to" + i + "]");
    if (elPrevious) {
        var valPrevious = $(elPrevious).val();
    }

    var final = (parseInt(value) <= parseInt(valPrevious));

    if (final == true) {
        return false;
    }
}, "my msg");

}); });

this code shows the message even if the condition is not met; 即使不满足条件,此代码也会显示该消息;

Can anyone give me a hint? 谁能给我一个提示?

You have no return true in your custom validation: 您的自定义验证没有返回true:

jQuery.validator.addMethod("checkPrevValuePaxTo", function (value, element) {

var el = element.className;
var temp = el.match(/\d+/g).map(Number);
var i = temp - 1;

var elPrevious = $("input[name=pax_to" + i + "]");
if (elPrevious) {
    var valPrevious = $(elPrevious).val();
}

return !(parseInt(value) <= parseInt(valPrevious));

}, "my msg");

In your custom method, if the conditional fails, nothing is returned... 在您的自定义方法中,如果条件失败,则不返回任何内容。

if (final == true) { 
    return false;
}  // otherwise return nothing?

Instead of adding an else or return true , simplify it greatly by eliminating the final variable and the conditional. 无需添加elsereturn true ,而是通过消除final变量和有条件的条件将其大大简化。 After all, there's not much point in comparing a boolean to a boolean. 毕竟,将布尔值与布尔值进行比较没有什么意义。

return !(parseInt(value) <= parseInt(valPrevious));

Also, you do not need to use var more than once... 另外,您不需要多次使用var ...

jQuery.validator.addMethod("checkPrevValuePaxTo", function (value, element) {

    var el = element.className,
        temp = el.match(/\d+/g).map(Number),
        i = temp - 1,
        elPrevious = $("input[name=pax_to" + i + "]"),
        valPrevious;

    if (elPrevious) {
        valPrevious = $(elPrevious).val();
    }

    return !(parseInt(value) <= parseInt(valPrevious));

}, "my msg");

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

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