javascript 如何使用javascript验证表单数组输入字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5537972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:33:17  来源:igfitidea点击:

how to validate form array input field using javascript

javascripthtmlforms

提问by Khurram Ijaz

i have input field i.e.

我有输入字段,即

form name='myform' action='' method='post' onsubmit= return validate(this); /
input type='text' name='sort_order[]' id='sortorder' 

i want to use javascript on submit button to check all of the inputs have integer value.

我想在提交按钮上使用 javascript 来检查所有输入是否具有整数值。

{<javascript>}

 function validate(obj)
{
     if(obj.elements['sort_order[]'].length == 0)
    {
        alert(" Please Enter Value!");  
        return false;
    }   


}

Please help. thanks

请帮忙。谢谢

回答by RobG

Andrew's solution is pretty good, however I'd suggest using a regular expression rather than parseInt, e.g.

安德鲁的解决方案非常好,但是我建议使用正则表达式而不是 parseInt,例如

form.onsubmit = function() {
  var re = /^\d+$/;

  for(var i = 0; i < form.elements.length; i++) {

    if(form.elements[i].type == "text" && !re.test(form.elements[i].value)) {
      ...

Because:

因为:

var s = '08';
parseInt(s) == s // false;

also integers of the form 2e3 will return false for both parseInt and RegExp tests. It depends on how robust or general the function needs to be.

对于 parseInt 和 RegExp 测试,2e3 形式的整数也将返回 false。这取决于函数需要的健壮性或通用性。

回答by Andrew Marsh

Ok, your description is a bit vague but here is one solution.

好的,您的描述有点模糊,但这是一种解决方案。

If your html looks like this

如果你的 html 看起来像这样

<!DOCTYPE html>
<html>
  <head>
    <title>Form</title>
  </head>
  <body>
    <form id = "important_form">
      <input type = "text" value = "0"/>
      <input type = "text" value = "0"/>
      <input type = "text" value = "0"/>
      <input type = "submit" value = "submit"/>
    </form>
    <script type = "text/javascript" src="validate.js"></script>
  </body>
</html>

Then you could use javascript similar to this

然后你可以使用类似于这个的javascript

form = document.getElementByID("important_form");
//This will execute when the user presses the submit button.
form.onsubmit = function() {
  //Loop through all form elements.
  for(var i = 0; i < form.elements.length; i++) {
    //If the form element is a text input and the value is not an integer
    if(form.elements[i].type == "text" && 
       parseInt(form.elements[i].value) != form.elements[i].value) {
      alert("Please enter an integer value"); //Replace this with whatever you want
                                              //to do with invalid results
      return false; //Stops the submit from continuing.
    }
  }
  return true;
}

回答by learner

     var k1=document.getElementsByName("version[]");
     var y1=k1.length;
     console.log(y1);
      for(var x=0;x<y1;x++)
            {
         if(k1[x].value==null||k1[x].value=="")
                {
                alert("please fill version");
              return false;
       }
         }