如何检查验证器是否在页面上验证和验证失败
How to check if the validator validated and failed validation on the page
我在页面上有多个验证器,当单击“提交”按钮时它们都可以正常工作
但是,当我通过下拉选择的索引更改事件启动回发时,我需要检查验证器是否失败,以便失败的验证消息正确保留
换句话说,我正在尝试检查某个验证器是否刚刚被触发并失败
我尝试了以下方法
- 我检查了 isValid 属性,但无论如何它总是正确的
- 我试图检查 Page.IsValid 方法,但没有前一个方法就失败了
Page.Validate() 调用
- 我尝试检查失败消息是否可见并存在,但没有
验证器的此类选项
因此,有没有办法检查所需的验证器是否刚刚被触发并失败了?
看起来很简单,但我仍然找不到解决方案
非常感谢您
想出了解决办法
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();
}
}
我在页面上有多个验证器,当单击“提交”按钮时它们都可以正常工作
但是,当我通过下拉选择的索引更改事件启动回发时,我需要检查验证器是否失败,以便失败的验证消息正确保留
换句话说,我正在尝试检查某个验证器是否刚刚被触发并失败
我尝试了以下方法
- 我检查了 isValid 属性,但无论如何它总是正确的
- 我试图检查 Page.IsValid 方法,但没有前一个方法就失败了 Page.Validate() 调用
- 我尝试检查失败消息是否可见并存在,但没有 验证器的此类选项
因此,有没有办法检查所需的验证器是否刚刚被触发并失败了?
看起来很简单,但我仍然找不到解决方案
非常感谢您
想出了解决办法
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();
}
}