Solved

Javascript validation for dynamically added textarea

Posted on 2003-11-30
3
1,287 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
Comment Utility
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
Comment Utility
gr8
;)
works perfectly fine
0
 
LVL 10

Expert Comment

by:NetGroove
Comment Utility
Thanks for the points.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

743 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

19 Experts available now in Live!

Get 1:1 Help Now