How can I implement textbox blank validation on button click within a gridview using JavaScript? My gridview has multiple rows with 2 textboxes and a save button in each row. I need to validate the textboxes when their corresponding save button is clicked.
I have attempted to create the validation logic, but it currently only works for hardcoded textBox ids. How can I modify this code to apply to all rows of the gridview?
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.includes('TextBoxCTTermCode'))
{
if (Inputs[i].value == "") {
alert("Please enter a value, blank input is not allowed");
return false;
}
}
else if (Inputs[i].id.includes('TextBoxCTTermDesc')) {
if (Inputs[i].value == "") {
alert("Please enter a value, blank input 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