簡體   English   中英

如何驗證 javascript 中的動態行數組值

[英]how to validate dynamic row array values in javascript

pl。 告訴我如何驗證 javascript 中的數組值。 我正在發布我目前正在使用的代碼。 問題是即使在填寫了所有值之后,我仍然不斷收到驗證錯誤消息。 pl。 告訴我我哪里錯了

function chkdate() {
    var x = document.forms["main"]["date"].value;
    if (x == null || x == "") {
        document.forms["main"]["date"].focus();
        document.forms["main"]["date"].style.background = 'red';
        alert("Date Cannot be empty");
        return false;
    }
}

function chkempty() {
    var len = document.forms["main"]["item[]"].length;
    if (len == undefined) {
        var ic = document.forms["main"]["item[]"].value;
        var iq = document.forms["main"]["qty[]"].value;
        var ip = document.forms["main"]["price[]"].value;
        if (ic == null || ic == "") {
            document.forms["main"]["item[]"].focus();
            document.forms["main"]["item[]"].style.background = 'red';
            alert("Item Cannot be empty");
            return false;
        }
        if (iq == null || iq == "") {
            document.forms["main"]["qty[]"].focus();
            document.forms["main"]["qty[]"].style.background = 'red';
            alert("Qty Cannot be empty");
            return false;
        }
        if (ip == null || ip == "") {
            document.forms["main"]["price[]"].focus();
            document.forms["main"]["price[]"].style.background = 'red';
            alert("Price Cannot be empty");
            return false;
        }
    } else for (i = 0; i < len; i++) {
        var ica = document.forms["main"]["item[]"][i].value;
        var iqa = document.forms["main"]["qty[]"][i].value;
        var ipa = document.forms["main"]["price[]"][i].value;
        if (ica == null || ica == "") {
            document.forms["main"]["item[]"][i].focus();
            document.forms["main"]["item[]"][i].style.background = 'red';
            alert("Item Cannot be empty");
            return false;
        }
        if (iqa == null || iqa == "") {
            document.forms["main"]["qty[]"][i].focus();
            document.forms["main"]["qty[]"][i].style.background = 'red';
            alert("Qty Cannot be empty");
            return false;
        }
        if (ipa == null || ipa == "") {
            document.forms["main"]["price[]"][i].focus();
            document.forms["main"]["price[]"][i].style.background = 'red';
            alert("Price Cannot be empty");
            return false;
        }
    }
}

其他細節是:-

表單名稱:主要輸入框項目,數量和價格是根據用戶要求的動態行。 謝謝大家。

我建議您首先將 JQuery 與驗證插件結合使用。

然后對於您的情況,請遵循這個對我來說很好的示例:

$("#testform").validate({
    rules: {'qty[]': {required: true,minlength: 1},
    //other rules here
},
messages: {
    'qty[]': "Select at least one qty",
    //other custom error msgs go here
}
});

<input name="qty[]" id="1" value="1" type="checkbox" /> 
<input name="qty[]" id="2" value="2" type="checkbox" /> 
<input name="qty[]" id="3" value="3" type="checkbox" /> 

請注意,數組值字段的名稱必須用引號 ('qty[]') 指定,否則會出現 js 錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM