admin管理员组

文章数量:1346324

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

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


}

Please help. thanks

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

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


}

Please help. thanks

Share Improve this question edited Apr 4, 2011 at 11:55 S L 14.3k18 gold badges79 silver badges119 bronze badges asked Apr 4, 2011 at 11:45 Khurram IjazKhurram Ijaz 1,8645 gold badges24 silver badges43 bronze badges 3
  • Use the {} button to format your code in the future. I took care of it, but did not fix any syntax. – Dutchie432 Commented Apr 4, 2011 at 11:46
  • use jquery validate plugin :) – S L Commented Apr 4, 2011 at 11:47
  • G molvi use {} (in text-editor) to format code. Your html tags will also be printed – S L Commented Apr 4, 2011 at 12:08
Add a ment  | 

3 Answers 3

Reset to default 4

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

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.

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

If your html looks like this

<!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

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

本文标签: htmlhow to validate form array input field using javascriptStack Overflow