Hi,

I am trying to validate textbox inside gridview footer template as the following:

<script language="javascript" type="text/javascript">
function validate(obj)
{
      if (document.getElementById(obj).value=="")
      {
                 alert("no data entered");
                 document.getElementById(obj).focus();
                 return false;
      }

     return true;
}
</script>

In the code behind, I added the following:

Dim InsertN As TextBox = TryCast(GridView1.FooterRow.FindControl("InsertN"), TextBox)
Dim Insert As LinkButton = TryCast(GridView1.FooterRow.FindControl("Insert"), LinkButton)
Insert.Attributes.Add("onclick", "return validate('" + InsertN.ClientID + "');")

Now, the problem is that I have more than one textbox control to validate.

Any idea what needs to be done in order to validate all textbox controls one by one?

Thanks.

Try to use the validate( /* this parameter can hold 3 classes and can be set to the following variables */ ((( this ) || ( this.id || 'formID' )) || 0 )); function provided in this demo along with your forms.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html id="html40L" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Window-target" content="_top">
<title>Free Live Help!</title>
<script type="text/javascript">
<!--

var validate = function( form ) {
var myform;
var eLen;
var x;

   switch( typeof( form ) ) {
   case "object" : myform = form; break;
   case "string" : myform = (( document.forms ) ? document.forms[ form ] : (( document.getElementById ) ? document.getElementById( form ) : document.all[ form ] )); break;
   case "number" : myform = (( document.getElementsByTagName ) ? document.getElementsByTagName("form")[ form ] : document.all.tags("form")[ form ] ); break;
   default : myform = undefined; break;
   } if ( typeof( form ) !== "undefined" ) {
      if ( myform.elements && document.forms ) {
      eLen = myform.elements.length;
         for ( x = 0; x < eLen; x++ ) {
            if (((( myform.elements[ x ].type )  === "text" ) || "password" ) &&  !( myform.elements[ x ].value )) {
            alert( "no data entered on the " + myform.elements[ x ].id + " field!" );
            myform.elements[ x ].focus();
            return false;
            } 
         } 
      } else { 
      var nodes = (( document.getElementsByTagName ) ? myform.getElementsByTagName("input") : myform.all.tags("input"));
      eLen = nodes.length;
         for ( x = 0; x < eLen; x++ ) {
            if (((( nodes[ x ].type ) === "text" ) || "password" ) && !( nodes[ x ].value )) {
            alert( "no data entered on the " + nodes[ x ].id + " field!" );
            nodes[ x ].focus();
            return false;
            }
         }     
      } return true;
   } alert( "Your browser is too old to handle this type of features" );
   return false;
};
// -->
</script>
</head>
<body>
<div id="main">
<form id="testform" name="testform" action="#" onsubmit="return validate( this.id );">
<div>
<label for="username">username: <input type="text" id="username" name="username" size="20" value=""></label><br>
<label for="password">password: <input type="password" id="password" name="password" size="20" value=""></label><br>
<label for="email">email: <input type="text" id="email" name="email" size="20" value=""></label><br>
<input type="submit" value="Confirm">
</div>
</form>
</div>
</body>
</html>

hope it helps...

This article has been dead for over six months. Start a new discussion instead.