Solved

Javascript validation for dynamically added textarea

Posted on 2003-11-30
3
1,288 Views
Last Modified: 2012-06-27
I am using the insRow() function to add rows to a table.
function insRow()
{
    var x=document.getElementById('myTable').insertRow(TRCount)
    var y=x.insertCell(0)
    var z=x.insertCell(1)
    showCount = TRCount + 1
    y.innerHTML=showCount
    z.innerHTML="<TEXTAREA style=overflow:hidden NAME='task"+TRCount+"' ROWS='' COLS='45'></TEXTAREA>"
    TRCount++;
}
But when I submit the form i need to make sure that none of the textarea is empty(Simple javascript validation).But it does not seem to work here.

function submitForm()
{
   var count = document.getElementById("mytable").rows.length

   for(var i=0; i<count; i++)
      {
         alert("task"+i.value)
         if("task'"+i+"'".value=="")
       {
                 alert("Please Enter Task");
                 return;
           }
          }
     form1.action="addAdminReport.jsp";
     form1.submit();
      
}
Please Help
0
Comment
Question by:sangeeta9189
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
NetGroove earned 0 total points
ID: 9848730
Here my proposal:

<html>
<head>
<script>
var TRCount=0;
function insRow(){
    var x=document.getElementById('myTable').insertRow(TRCount)
    var y=x.insertCell(0)
    var z=x.insertCell(1)
    showCount = TRCount + 1
    y.innerHTML=showCount
    z.innerHTML="<TEXTAREA style='overflow:hidden' NAME='task"+TRCount+"' ROWS='' COLS='45'></TEXTAREA>"
    TRCount++;
}

function submitForm(theForm){
   elem = theForm.elements;
   for(var i=0; i<elem.length; i++){
     if(elem[i].type=='textarea' && elem[i].name.substr(0,4)=='task'){
       alert(elem[i].name+": "+elem[i].value) // only for debugging
       if(elem[i].value==""){
         alert("Please Enter Task");
         elem[i].focus();
         return false;
       }
     }
   }
   theForm.action="addAdminReport.jsp";
   return true;
}
</script>
</head>
<body>
<form name="form1" onSubmit="return submitForm(this)">
<table id="myTable">
</table>
<input type=button value="Add" onClick="insRow()">
<input type=submit>
</body>
</html>


0
 

Expert Comment

by:anshul9189
ID: 9848786
gr8
;)
works perfectly fine
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9848880
Thanks for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

896 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

14 Experts available now in Live!

Get 1:1 Help Now