在单个JavaScript函数中验证多个文本框

时间:2017-05-08 10:22:29

标签: javascript asp.net validation

  

我有7个文本框,我想验证为必填字段OnClientClick   单击名为Update的按钮   所以有什么方法可以使用Javascript来做到这一点   我已经尝试过这段代码,但它并没有作为其中之一   ps:我是JS的新手

   function fnCheck(val) {
            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();
            if (merter != '') {
                return true;
            }
            else {
                alert("Meter Identifier is Required Field");
            return false;
        }

        if (Billper != '') {
            return true;
        }
        else {
            alert("Bill Period is Required Field");
            return false;
        }

        if (Endkwh != '') {
            return true;
        }
        else {
            alert("EndKwh is Required Field");
            return false;
        }
        if (startkwh != '') {
            return true;
        }
        else {
            alert("StartKwh is Required Field");
            return false;
        }
        if (ReadEndDate != '') {
            return true;
        }
        else {
            alert("Read EndDate is Required Field");
            return false;
        }

        if (ReadStartDate != '') {
            return true;
        }
        else {
            alert("Read StartDate is Required Field");
            return false;
        }
        if (CTFACT != '') {
            return true;
        }
        else
        { alert("CT Factor is Required Field");
        return false;
        }
        }
 <asp:Button ID="btn_Update" Style="background-color: #B2DE94; width: 40px"  CausesValidation="false" runat="server"  OnClientClick="fnCheck(this);" Text="Update" CommandName="Update" />

2 个答案:

答案 0 :(得分:0)

尝试以下

<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>

代码来自here

我已通过示例项目验证并适用于您的方案

答案 1 :(得分:0)

  
    

好吧我已经处理了我的JS功能,它对我有用。我会这样做     为其他人工作。在我的代码中,我采取了一个变量

  
     

success作为一个标志,我正在检查它两次   最后返回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;

    }

<强>的OnClientClick

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