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.
如何在javascript中对gridview中的按钮单击应用文本框空白验证?我有一个gridview,每行包含两个文本框和一个save按钮。我要在相应的save按钮单击上验证文本框。
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?
我已经应用了这个逻辑,但问题是它只适用于硬编码的文本框id。我如何修改该代码,以便它能适用于所有的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 == '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
3 个解决方案
#1
2
Finally I got the solution of my problem..I have just passed index of row of gridview to javascript function.
我终于找到解决我问题的办法了。我刚刚将gridview的行索引传递给javascript函数。
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
#2
1
Don't check for id. Simply check for blank value.
不要检查id,只需检查空值即可。
if(Inputs[i].type == 'text' )
{
if (Inputs[i].value == "") {
alert("Enter values,blank is not allowed");
return false;
}
}
#3
0
<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
您可以使用验证控件来验证任何输入
#1
2
Finally I got the solution of my problem..I have just passed index of row of gridview to javascript function.
我终于找到解决我问题的办法了。我刚刚将gridview的行索引传递给javascript函数。
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
#2
1
Don't check for id. Simply check for blank value.
不要检查id,只需检查空值即可。
if(Inputs[i].type == 'text' )
{
if (Inputs[i].value == "") {
alert("Enter values,blank is not allowed");
return false;
}
}
#3
0
<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
您可以使用验证控件来验证任何输入