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
Solved

Javascript validation for dynamically added textarea

Posted on 2003-11-30
3
1,291 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JS does not refresh 6 30
check a checkbox of a checkboxes group in bootstrap 2 42
binding event to form in window.open 9 37
Start Auto-download of File when page loads? 1 22
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

860 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