2011-05-30 38 views
0

pl。建议我如何验证JavaScript中的数组值。 我张贴我目前正在使用的代码。问题是,即使填满了所有的值,我仍然不断收到验证错误消息。 pl。劝我在哪里,我错了如何验证javascript中的动态行数组值

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; 
     } 
    } 
} 

其他细节是: -

形式名称:主要 输入框项目,数量和价格是根据用户的需求动态行。 谢谢大家。

+1

请以可读的方式缩进您的源代码。使用四个空格而不是制表符。 – Oswald 2011-05-30 12:31:32

+0

@polarblau 我使用了你编辑过的代码,但它给出了同样的错误。 – mmdel 2011-05-30 13:08:11

+0

我刚刚对代码进行了格式化以提高可读性,而不是更改其他任何内容。在这里你可以看到我做了什么,为什么:http://stackoverflow.com/posts/6176288/revisions – polarblau 2011-05-30 13:11:11

回答

0

我建议你作为第一点与验证插件一起使用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错误。