0

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

3
  • Use the {} button to format your code in the future. I took care of it, but did not fix any syntax. Commented Apr 4, 2011 at 11:46
  • use jquery validate plugin :) Commented Apr 4, 2011 at 11:47
  • G molvi use {} (in text-editor) to format code. Your html tags will also be printed Commented Apr 4, 2011 at 12:08

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

3 Comments

should add return true after cycle and != instead of ! ... ==
Thanks, typed this out quite quickly
still wrong :) !== will be always false because form.elements[i].value is a string anyway. And <= form.elements.length is wrong too
0
     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;
       }
         }

2 Comments

Can you explain your answer ?
if <input type="text" name=version[]> and you have dynamically appended multiple inputs by using jquery append, then k1 is an array of all version values. Inorder to check if all the values of version are not null follow the above procedure. y1 gives number of dynamic inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.