Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript Form Validate rows in HTML table
    primarykey
    data
    text
    <p>For some reasons my javascript form validation does not go through every row in the table. I created the table with the codes below: The $usr variable is just a result of another process:</p> <pre><code>&lt;form name="myForm" action="addAccessories.php" method="post" onsubmit="return validateForm(this);"&gt; &lt;table border="1" id="IT"&gt; &lt;tr&gt; &lt;th&gt;Barcode&lt;em&gt;*&lt;/em&gt;&lt;/th&gt; &lt;th&gt;Current stock&lt;/th&gt; &lt;/tr&gt; &lt;?php for($id=1; $id&lt;=$usr; $id++){ ?&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="hidden" name="id[]" value="&lt;?php echo $id; ?&gt;" /&gt; &lt;input type="text" name="bar_code&lt;?php echo $id; ?&gt;" id="bar_code&lt;?php echo $id; ?&gt;" value="" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="num_stock&lt;?php echo $id; ?&gt;" value="0" id="num_stock&lt;?php echo $id; ?&gt;"/&gt;&lt;/td&gt; &lt;?php } ?&gt; &lt;tr&gt; &lt;td&gt; &lt;/td&gt; &lt;td&gt; &lt;button data-theme="b" input type="submit" name="Submit" value="Submit"&gt;Add accessories&lt;/button&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p>The number of rows from this table is: rows = $usr + 1</p> <p>My validation form codes:</p> <pre><code>&lt;script type="text/javascript"&gt; function validateForm(){ var errors = []; for(i = 1; i &lt; document.getElementById("IT").rows.length; i++){ var barcode = document.getElementById('bar_code'+i).value; var des = document.getElementById('description'+i).value; var num_stock = document.getElementById('num_stock'+i).value; if(barcode == null || barcode == ""){ errors[errors.length] = "You must enter barcode."; } if(isNaN(num_stock)){ errors[errors.length] = "Current stock must be an integer"; } if(num_stock &lt; 0){ errors[errors.length] = "Current stock must be a positive number"; } if (errors.length &gt; 0) { reportErrors(errors); return false; } return true; } } function reportErrors(errors){ var msg = "There were some problems...\n"; for (var i = 0; i&lt;errors.length; i++) { var numError = i + 1; msg += "\n" + numError + ". " + errors[i]; } alert(msg); } &lt;/script&gt; </code></pre> <p>This process only validates the 1st row in the table then stop. Can anyone show me what went wrong and how to fix it? Thank you very much for your help</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload