ASP中GridView中TextBox的JavaScript验证网

本文关键字:验证 JavaScript TextBox GridView ASP | 更新日期: 2023-09-27 17:58:30

我有一个GridView,其中Template字段中有四个TextBox。我在GridView下面有一个按钮。

单击按钮时,如何验证GridView中的文本框?

ASP中GridView中TextBox的JavaScript验证网

使用RequiredFieldValidator并设置ValidationGroup="gridview",检查以下示例

   <asp:TemplateField HeaderText="">
          <ItemTemplate>
              <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
               <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="TextBox3" ValidationGroup="gridview" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
          </ItemTemplate>
            </asp:TemplateField>
          <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="gridview" CausesValidation="true" />
                </ItemTemplate>
            </asp:TemplateField>

您可以使用JQuery验证插件

<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate  /lib/jquery.delegate.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
<script type="text/javascript">
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});;
</script>
<script>
$(document).ready(function(){
$("#myform").validate({
rules: {
field: "required"
}
});
});

 <body>
 <form id="myform">
  <label for="field">Required: </label>
  <input class="left" id="field" name="field" />
  <br/>
  <input type="submit" value="Validate!" />
 </form>
 </body>
<script type="text/javascript">
    function ValidateGridview() {
        titlename = document.getElementById('<%=((TextBox)grd_party_influenc.FooterRow.FindControl("txt_f_title")).ClientID%>');
        if (titlename.value ==0) {
            alert("Please Insert The Title ....");
            titlename.focus();
            return false;
        }
       // return true;
    }
</script>

然后通过如下链接按钮调用JavaScript函数:

<asp:LinkButton ID="lnk_btn_insert" runat="server" CommandName="Insert" OnClientClick="ValidateGridview()">Insert</asp:LinkButton>

我有7个文本框

好的,我已经完成了JS函数,它对我有效。希望它能为其他人工作。在我的代码中,我使用了一个可变

success,它是一个标志,我正在检查它两次在末尾返回true,以便如果其中一个文本框不为空否则它将不会返回true。很抱歉编辑不好

 function fnCheck(val) {
        var success = true;
        var v = val.id.split('_')[1];
        var merter = document.getElementById('GridSubMeter_' + v + '_txtMeterIdn').value.trim();
        var Billper = document.getElementById('GridSubMeter_' + v + '_txBillPer').value.trim()
        var Endkwh = document.getElementById('GridSubMeter_' + v + '_txEndKwh').value.trim();
        var startkwh = document.getElementById('GridSubMeter_' + v + '_txStartKwh').value.trim();
        var ReadEndDate = document.getElementById('GridSubMeter_' + v + '_txReadEndDate').value.trim();
        var ReadStartDate = document.getElementById('GridSubMeter_' + v + '_txReadStartDate').value.trim();
        var CTFACT = document.getElementById('GridSubMeter_' + v + '_txCTFact').value.trim();
        debugger;
        if (merter != '') {
        }
        else {
            alert("Meter Identifier is Required Field");
            success = false;
    }
    if (Billper != '') {
    }
    else {
        alert("Bill Period is Required Field");
        success = false;
    }
    if (Endkwh != '') {
    }
    else {
        alert("EndKwh is Required Field");
        success = false;
    }
    if (startkwh != '') {
    }
    else {
        alert("StartKwh is Required Field");
        success = false;
    }
    if (ReadEndDate != '') {
    }
    else {
        alert("Read EndDate is Required Field");
        success = false;
    }
    if (ReadStartDate != '') {
    }
    else {
        alert("Read StartDate is Required Field");
        success = false;
    }
    if (CTFACT != '') {
    }
    else
    { alert("CT Factor is Required Field");
    success = false;
}
return success;
    }

客户端点击

<asp:Button ID="btn_Update" Style="background-color: #B2DE94; width: 40px"  CausesValidation="false" runat="server"  OnClientClick="return fnCheck(this);" Text="Update" CommandName="Update" />