Solved

Javascript validation for dynamically added textarea

Posted on 2003-11-30
3
1,295 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
[X]
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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

695 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