如何在页面上检查验证程序是否已验证和验证失败

时间:2018-12-04 14:44:21

标签: c# asp.net requiredfieldvalidator

页面上有多个验证器,当单击“提交”按钮时,所有验证器都可以正常工作

但是,当我通过下拉选定索引更改事件发起回发时,我需要检查验证器是否失败,以便正确保留失败的验证消息

换句话说,我正在尝试检查某个验证程序是否刚刚被触发并失败了

我尝试了以下

  1. 我检查了isValid属性,但无论如何始终为真
  2. 我尝试检查Page.IsValid方法,但是如果没有以前的方法,它将失败 Page.Validate()调用
  3. 我尝试检查失败的消息是否可见并存在,但是没有 验证者的这种选择

因此,有没有办法检查所需的验证程序是否刚刚被触发并失败?

这似乎很简单,但我仍然找不到解决方法

非常感谢您

1 个答案:

答案 0 :(得分:0)

提出解决方案

HTML

    <asp:UpdatePanel runat="server" id="UpdatePanel1">
    <ContentTemplate>

    <table class="innerTable" border="0">
      <tr>
    <td>
        <asp:DropDownList  CssClass="textboxwidth" runat="server" ID="ddOrg" AutoPostBack="true" OnSelectedIndexChanged="ddOrg_SelectedIndexChanged" setCausesValidation="true"></asp:DropDownList>
        <asp:RequiredFieldValidator SetFocusOnError="true" ID="RequiredFieldValidator3" runat="server" Display="Dynamic" ControlToValidate="ddOrg" InitialValue="" ErrorMessage="* Required"></asp:RequiredFieldValidator>
        <input type="hidden" id="hdFirmValidator" runat="server" />
        <input type="hidden" id="hdPhoneValidator" runat="server" />
        <input type="hidden" id="hdPhoneValidatorRegex" runat="server" />
    </td>
</tr>
<tr>
    <td>
        <asp:TextBox ID="txtOrgOther" Enabled="false" CssClass="textboxwidth" MaxLength="100" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator SetFocusOnError="true" ID="RequiredFieldValidator12" Enabled="false" runat="server" Display="Dynamic" ControlToValidate="txtOrgOther" ErrorMessage="* Required" ></asp:RequiredFieldValidator>
    </td>
</tr>
<tr>
    <td>
        <asp:TextBox CssClass="textboxwidth" ID="txtOrgAddress" TextMode="MultiLine" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator SetFocusOnError="true" ID="RequiredFieldValidator14" runat="server" Display="Dynamic" ControlToValidate="txtOrgAddress" ErrorMessage="* Required" ></asp:RequiredFieldValidator>
    </td>
</tr>
<tr>
<td>
<asp:TextBox CssClass="textboxwidth" ID="txtOrgPhone" runat="server" MaxLength="30"></asp:TextBox>
<asp:RequiredFieldValidator SetFocusOnError="true" ID="RequiredFieldValidator15" runat="server" Display="Dynamic" ControlToValidate="txtOrgPhone" ErrorMessage="* Required" ></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexPhone1" ValidationExpression="^.{0,30}" ErrorMessage="*Enter upto 30 digit phone number" runat="server" ControlToValidate="txtOrgPhone"></asp:RegularExpressionValidator>
</td>
              </tr>
       </table>
</ContentTemplate>

 

JavaScript

    window.onload = function () {

    var btnSubmit = document.getElementById("<%=btnSubmit.ClientID%>");
    function SetHiddenFields() {

    var firmValidator = document.getElementById("<%=RequiredFieldValidator14.ClientID%>");
    var hdFirmValidator = document.getElementById("<%=hdFirmValidator.ClientID%>");
    hdFirmValidator.value = firmValidator.style.display;

    var phoneValidator = document.getElementById("<%=RequiredFieldValidator15.ClientID%>");
    var hdPhoneValidator = document.getElementById("<%=hdPhoneValidator.ClientID%>");
    hdPhoneValidator.value = phoneValidator.style.display;

    var phoneValidatorRegEx = document.getElementById("<%=regexPhone1.ClientID%>");
    var hdPhoneValidatorRegex = document.getElementById("<%=hdPhoneValidatorRegex.ClientID%>");
    hdPhoneValidatorRegex.value = phoneValidatorRegEx.style.display;

    return true;

}

btnSubmit.onclick = SetHiddenFields; }

隐藏代码

    protected void ddOrg_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Srver side code

        RequiredFieldValidator3.Validate();

        if (hdFirmValidator.Value != string.Empty)
        {
            RequiredFieldValidator14.Validate();
        }

        if (hdPhoneValidator.Value != string.Empty)
        {
            RequiredFieldValidator15.Validate();
        }

        if (hdPhoneValidatorRegex.Value != string.Empty)
        {
            regexPhone1.Validate();
        }

    }