validation using javascript of multiple add rows

Posted on 2011-03-15
Medium Priority
Last Modified: 2013-12-13

I want validation of mandatory for below attached script,

I have name field like name="txt[]",Its contains multiple rows of answer..
I want to validate all added field .

<SCRIPT language="javascript">
        function addRow(tableID) {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
            var colCount = table.rows[0].cells.length;
            for(var i=0; i<colCount; i++) {
                var newcell = row.insertCell(i);
                newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;

<TABLE id="dataTable" width="350px" border="1">
            <TD><INPUT type="checkbox" name="chk[]"/></TD>
            <TD><INPUT type="text" name="txt[]"/></TD>
                <SELECT name="country[]">
                    <OPTION value="india">India</OPTION>
                    <OPTION value="germany">Germany</OPTION>
                    <OPTION value="france">France</OPTION>
                    <OPTION value="usa">United States</OPTION>
                    <OPTION value="china">china</OPTION>

Open in new window

Question by:vravi22
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 35146124
 <TD><INPUT type="text" name="txt[]" id="txt"/></TD>

You can try assigning the id to the input field, so you can get the input field by ID

var obj= document.getElementById("txt");

alert("Please enter a value");
return false;

You can try same for other fields also.

Hope this helps

Author Comment

ID: 35146638
same thing i also tried but problem is when 2nd row  is not validated.. here id is txt validating for only 1 st row.
I want to increment id according to the add row..
LVL 17

Accepted Solution

Shinesh Premrajan earned 1500 total points
ID: 35146691
Hope this helps
var obj= document.getElementsByName('txt');
 for(var i=0;i<obj.length;i++){
		alert("Please enter a value");

Open in new window


Author Comment

ID: 35146980
This is worked for me .. I have changed txt to txt[] then taking whole array..

Thank you..

Author Closing Comment

ID: 35146990
This solution worked for me..

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question