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
Use the {} button to format your code in the future. I took care of it, but did not fix any syntax. – Dutchie432 Apr 4 '11 at 11:46
use jquery validate plugin :) – experimentX Apr 4 '11 at 11:47
G molvi use {} (in text-editor) to format code. Your html tags will also be printed – experimentX Apr 4 '11 at 12:08

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer

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;
}
share|improve this answer
1  
should add return true after cycle and != instead of ! ... == – Emmerman Apr 4 '11 at 12:10
Thanks, typed this out quite quickly – Andrew Marsh Apr 4 '11 at 12:12
1  
still wrong :) !== will be always false because form.elements[i].value is a string anyway. And <= form.elements.length is wrong too – Emmerman Apr 4 '11 at 12:13
Thanks, should work now. – Andrew Marsh Apr 4 '11 at 12:14
1  
Now replace <= with < :) – Emmerman Apr 4 '11 at 12:14
show 1 more comment

how to do with below example ?

alert(document.myform.sort_order[0].value);

as it gives error

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.