Solved

validation using javascript of multiple add rows

Posted on 2011-03-15
5
954 Views
Last Modified: 2013-12-13
Hello,

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;
                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }
            }
        }

<TABLE id="dataTable" width="350px" border="1">
        <TR>
            <TD><INPUT type="checkbox" name="chk[]"/></TD>
            <TD><INPUT type="text" name="txt[]"/></TD>
            <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>
                </SELECT>
            </TD>
        </TR>
    </TABLE>

Open in new window

javascriptvalidataion.png
0
Comment
Question by:vravi22
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:shinuq
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");

if(obj.value==''){
alert("Please enter a value");
return false;
}

You can try same for other fields also.

Hope this helps
0
 

Author Comment

by:vravi22
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..
0
 
LVL 17

Accepted Solution

by:
shinuq earned 500 total points
ID: 35146691
Hope this helps
var obj= document.getElementsByName('txt');
 for(var i=0;i<obj.length;i++){
	if(obj[i].value==''){
		alert("Please enter a value");
		}
}

Open in new window

0
 

Author Comment

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


Thank you..
0
 

Author Closing Comment

by:vravi22
ID: 35146990
This solution worked for me..
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now