0

我正在使用 jQuery 验证插件来检查 3 个日期输入的序列(例如)是否按升序排列。我的方法是编写一个循环,从上到下比较输入日期(首先检查第一个和第二个,然后检查第二个和第三个)。

我的问题:

  1. 我需要应用此验证规则多少次?目前,我将此规则应用于第一个输入。不过,它检查了我的 3 个输入是否按顺序排列。错误消息总是显示在第一个输入旁边。无论错误的位置。
  2. 我是否应该将函数修改为仅比较自身输入与其后继输入,并将其应用于除最后一个输入之外的所有输入?

更新一个新问题...如果我回答问题 2。我需要为必要的元素应用此验证规则 n-1 次(我也可以构建一个新类)。我的新问题是如何告诉 jQuery 应该比较哪两个日期(第一次检查日期 1 和日期 2,第二次检查日期 2 和日期 3)? 演示

HTML(具有三个日期输入的表格)

<form id="form1"><table><tr class = "app_dates"><th><label for = "id_Date_apt"> Application Date 1 (MM/DD): </label></th> <td> <input id = "id_Date_apt" type = "text" value = "01/11" name = "Date_apt"/></td></tr>
<tr class = "app_dates"><th><label for = "id_Date_apt"> Application Date 2 (MM/DD): </label></th> <td> <input id = "id_Date_apt2" type = "text" value = "12/12" name = "Date_apt2"/></td></tr>
<tr class = "app_dates"><th><label for = "id_Date_apt"> Application Date 3 (MM/DD): </label></th> <td> <input id = "id_Date_apt3" type = "text" value = "03/13" name = "Date_apt3"/></td></tr>
<tr><td><input type="submit" value="Submit"><input type="reset" value="Reset"></td></tr></table></form>

JS

$(document).ready(function() {
    //create a function used compare the sequence of input values
    function isDate() {
    var siz = $('input[id^="id_Date_"]').size()
    date = []
    temp_f = 0

// this for loop compares the secquences of application dates in pairs    
    for (var i = 0; i < siz - 1; i++) {
        j = i + 1
        var date1_m = parseFloat($('input[id^="id_Date_apt"]:eq(' + i + ')').val().slice(0, 2))
        var date2_m = parseFloat($('input[id^="id_Date_apt"]:eq(' + j + ')').val().slice(0, 2))
        var date1_d = parseFloat($('input[id^="id_Date_apt"]:eq(' + i + ')').val().slice(3, 5))
        var date2_d = parseFloat($('input[id^="id_Date_apt"]:eq(' + j + ')').val().slice(3, 5))
        var date1_full = new Date(1960, date1_m - 01, date1_d)
        var date2_full = new Date(1960, date2_m - 01, date2_d)

        if (date1_full > date2_full) {
            temp = 0
        }
        else {
            temp = 1
        }
        temp_f = temp_f + temp
    } //end of for loop

    if (temp_f != siz-1) {
        return false;
    }
    else {
        return true;
    }
}

//validation////
$.validator.addMethod("dateFormat", function(value, element) {
    return isDate();
}, "Inputs are not in sequence");

$("#form1").validate({
    submitHandler: function(form) {
        SubmittingForm()
    },
    rules: {
        //should I add the validation rule for Date_apt2 and Date_apt3??    
        Date_apt: {
            dateFormat: true
        }
    }
})

})​

​</p>

4

1 回答 1

0

What I would do is check each date against its predecessor. Since people usually fill in forms from top down, I'd consider the second date in a pair to be the wrong when when they're out of order. But either way is fine.

You don't need to put an explicit rule for each date in the .validate() call. If you give a form element a class name that matches a validation method, the method will be called automatically. So just give all but the last or first date_aptN elements the class dateFormat.

于 2012-09-13T23:51:55.660 回答