How to apply textbox blank validation on button click inside gridview in javascript?I have a gridview that contains 2 textboxes and a save button in each row.I want to validate the textboxes on corresponding save button click.
I have applied the logic but problem is that it will only work for textBox ids that are hardcoded.How can I modify this code so that it will work for all the gridview rows?
function gvValidate() {
var grid = document.getElementById('<%= GridViewCTInformation.ClientID %>');
if(grid!=null)
{
var Inputs = grid.getElementsByTagName("input");
for(i = 0; i < Inputs.length; i++)
{
if(Inputs[i].type == 'text' )
{
if (Inputs[i].id == 'ctl00_contentPlaceHolderSubScreen_GridViewCTInformation_ctl02_TextBoxCTTermCode')
{
if (Inputs[i].value == "") {
alert("Enter values,blank is not allowed");
return false;
}
}
else if (Inputs[i].id == 'ctl00_contentPlaceHolderSubScreen_GridViewCTInformation_ctl02_TextBoxCTTermDesc') {
if (Inputs[i].value == "") {
alert("Enter values,blank is not allowed");
return false;
}
}
}
}
return true;
}
}
Protected Sub GridViewTaxInformation_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewTaxInformation.RowDataBound
Try
If e.Row.RowType = DataControlRowType.DataRow Then
Dim btnSave As Button = DirectCast(e.Row.FindControl("ButtonSave"), Button)
btnSave.Attributes.Add("onclick", "return gvValidate()")
End If
Catch ex As Exception
Common.WriteLog(ex.Message)
Common.WriteLog((ex.StackTrace))
Response.Redirect("..\Errors.aspx", False)
End Try
End Sub
How to apply textbox blank validation on button click inside gridview in javascript?I have a gridview that contains 2 textboxes and a save button in each row.I want to validate the textboxes on corresponding save button click.
I have applied the logic but problem is that it will only work for textBox ids that are hardcoded.How can I modify this code so that it will work for all the gridview rows?
function gvValidate() {
var grid = document.getElementById('<%= GridViewCTInformation.ClientID %>');
if(grid!=null)
{
var Inputs = grid.getElementsByTagName("input");
for(i = 0; i < Inputs.length; i++)
{
if(Inputs[i].type == 'text' )
{
if (Inputs[i].id == 'ctl00_contentPlaceHolderSubScreen_GridViewCTInformation_ctl02_TextBoxCTTermCode')
{
if (Inputs[i].value == "") {
alert("Enter values,blank is not allowed");
return false;
}
}
else if (Inputs[i].id == 'ctl00_contentPlaceHolderSubScreen_GridViewCTInformation_ctl02_TextBoxCTTermDesc') {
if (Inputs[i].value == "") {
alert("Enter values,blank is not allowed");
return false;
}
}
}
}
return true;
}
}
Protected Sub GridViewTaxInformation_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewTaxInformation.RowDataBound
Try
If e.Row.RowType = DataControlRowType.DataRow Then
Dim btnSave As Button = DirectCast(e.Row.FindControl("ButtonSave"), Button)
btnSave.Attributes.Add("onclick", "return gvValidate()")
End If
Catch ex As Exception
Common.WriteLog(ex.Message)
Common.WriteLog((ex.StackTrace))
Response.Redirect("..\Errors.aspx", False)
End Try
End Sub
Share
Improve this question
edited May 11, 2012 at 15:18
Priyansh
asked May 11, 2012 at 7:12
PriyanshPriyansh
1471 gold badge5 silver badges13 bronze badges
3
- what do you mean by for all the rows ? – bassem ala Commented May 11, 2012 at 7:19
- Actually I have gridview in which on each row I have 2 textboxes and a save button.I want to validate that particular record whose save button is clicked. – Priyansh Commented May 11, 2012 at 7:24
- why don't you use the validation control like : RequiredFieldValidator – bassem ala Commented May 11, 2012 at 7:35
4 Answers
Reset to default 2Finally I got the solution of my problem..I have just passed index of row of gridview to javascript function.
Here's the code
function gvValidate(rowIndex) {
var grid = document.getElementById('<%= GridViewCTInformation.ClientID %>');
if(grid!=null) {
var Inputs = grid.rows[rowIndex + 1].getElementsByTagName("input");
for(i = 0; i < Inputs.length; i++)
{
if(Inputs[i].type == 'text' )
{
if (Inputs[i].value == "") {
alert("Enter values,blank is not allowed");
return false;
}
}
}
return true;
}
}
Protected Sub GridViewCTInformation_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewCTInformation.RowDataBound
Try
If e.Row.RowType = DataControlRowType.DataRow Then
Dim btnSave As Button = DirectCast(e.Row.FindControl("ButtonCTInfoSave"), Button)
btnSave.Attributes.Add("onclick", "return gvValidate(" + e.Row.RowIndex.ToString() + ")")
End If
Catch ex As Exception
Common.WriteLog(ex.Message)
Common.WriteLog((ex.StackTrace))
Response.Redirect("..\Errors.aspx", False)
End Try
End Sub
Don't check for id. Simply check for blank value.
if(Inputs[i].type == 'text' )
{
if (Inputs[i].value == "") {
alert("Enter values,blank is not allowed");
return false;
}
}
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="the name of your textbox to be validated" ForeColor="Red"></asp:RequiredFieldValidator>
try it may help u you may use the validation controls to validate any input
<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=GridView1] [id*=lnkUpdate]").click(function () {
//Find the GridView Row using the LinkButton reference.
var row = $(this).closest("tr");
//Find the TextBox control.
var txtName = row.find("[id*=txtName]");
//Find the DropDownList control.
var ddlCountries = row.find("[id*=ddlCountries]");
var message = "";
//Validate the TextBox control.
if ($.trim(txtName.val()) == "") {
message += "Please enter Name.\n";
}
//Validate the DropDownList control.
if (ddlCountries.val() == "") {
message += "Please select Country.";
}
//Display error message.
if (message != "") {
alert(message);
return false;
}
return true;
});
});
</script>
You may try above code for Grid View Validation.