使用 asp.net 验证器验证 2 个字段中的 1 个

发布于 2024-08-20 19:44:55 字数 127 浏览 6 评论 0原文

我有一个带有一些文本框的简单表单。这些文本框都有一个RequiredFieldValidator。如果填写了字段 1,我需要禁用字段 2 的RequiredFieldValidator,因为只需要这些字段之一。实现这一目标的最佳方法是什么?

I've got a simple form with some textboxes. These textboxes all have a RequiredFieldValidator. If field 1 is filled in I need to disable the RequiredFieldValidator of field 2 because only one of these fields is required. What is the best way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

執念 2024-08-27 19:44:55

在这种情况下,使用 CustomValidator 带有服务器端验证处理程序。您应该将自定义验证器添加到两个控件:

<asp:TextBox runat="server" id="control1" />
<asp:CustomValidator runat="server" id="cusCustom1" controltovalidate="control1" onservervalidate="cusCustom_ServerValidate" errormessage="your message" />
<asp:TextBox runat="server" id="control2" />
<asp:CustomValidator runat="server" id="cusCustom2" controltovalidate="control2" onservervalidate="cusCustom_ServerValidate" errormessage="your message" />

并实现处理程序,例如

protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{
    e.IsValid = (!string.IsNullOrempty(control1.Text)) || (!string.IsNullOrempty(control2.Text))
}

In this situation it's simpler to use CustomValidator with server side validating handler. You should add the custom validator to both controls:

<asp:TextBox runat="server" id="control1" />
<asp:CustomValidator runat="server" id="cusCustom1" controltovalidate="control1" onservervalidate="cusCustom_ServerValidate" errormessage="your message" />
<asp:TextBox runat="server" id="control2" />
<asp:CustomValidator runat="server" id="cusCustom2" controltovalidate="control2" onservervalidate="cusCustom_ServerValidate" errormessage="your message" />

and implement the handler like

protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{
    e.IsValid = (!string.IsNullOrempty(control1.Text)) || (!string.IsNullOrempty(control2.Text))
}
甜是你 2024-08-27 19:44:55

目前我已经用 javascript 解决了这个问题,并且可以使用普通的验证器。

<script language="javascript" type="text/javascript"> 
function CheckPhoneValidator(txtEmail)
{
    var phoneValidator = document.getElementById('<%= ReqPhone.ClientID %>');
    ValidatorEnable(phoneValidator, txtEmail.value == '' ? true : false);
}

function CheckEmailValidator(txtPhone)
{
    var emailValidator = document.getElementById('<%= ReqEmail.ClientID %>');
    var emailRegexValidator = document.getElementById('<%= RegexEmail.ClientID %>');
    ValidatorEnable(emailValidator, txtPhone.value == '' ? true : false);
    ValidatorEnable(emailRegexValidator, txtPhone.value == '' ? true : false);
}

这些是控件:

<tr>
    <td>
        E-mail adres:
    </td>
    <td>
        <asp:TextBox ID="TxtEmail" runat="server" onchange="javascript:CheckPhoneValidator(this);"></asp:TextBox>
        <asp:RequiredFieldValidator ID="ReqEmail" runat="server" ControlToValidate="TxtEmail" ErrorMessage="U moet een e-mail invullen als u geen telefoonnummer heeft ingevuld." Display="Dynamic" ValidationGroup="Contact"> </asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegexEmail" runat="server" ControlToValidate="TxtEmail" ErrorMessage="Dit is geen geldig e-mail adres." Display="Dynamic"  ValidationExpression="([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})" ValidationGroup="Contact"> </asp:RegularExpressionValidator>
    </td>
</tr>
<tr>
    <td>
        Telefoonnummer:
    </td>
    <td>
        <asp:TextBox ID="TxtPhone" runat="server" onchange="javascript:CheckEmailValidator(this);"></asp:TextBox>
        <asp:RequiredFieldValidator ID="ReqPhone" runat="server" ControlToValidate="TxtPhone" ErrorMessage="U moet een telefoonnummer invullen als u geen e-mail heeft ingevuld." Display="Dynamic" ValidationGroup="Contact"> </asp:RequiredFieldValidator>
    </td>
</tr>

Currently I've solved it with javascript and I can use normal validators.

<script language="javascript" type="text/javascript"> 
function CheckPhoneValidator(txtEmail)
{
    var phoneValidator = document.getElementById('<%= ReqPhone.ClientID %>');
    ValidatorEnable(phoneValidator, txtEmail.value == '' ? true : false);
}

function CheckEmailValidator(txtPhone)
{
    var emailValidator = document.getElementById('<%= ReqEmail.ClientID %>');
    var emailRegexValidator = document.getElementById('<%= RegexEmail.ClientID %>');
    ValidatorEnable(emailValidator, txtPhone.value == '' ? true : false);
    ValidatorEnable(emailRegexValidator, txtPhone.value == '' ? true : false);
}

And these are the controls:

<tr>
    <td>
        E-mail adres:
    </td>
    <td>
        <asp:TextBox ID="TxtEmail" runat="server" onchange="javascript:CheckPhoneValidator(this);"></asp:TextBox>
        <asp:RequiredFieldValidator ID="ReqEmail" runat="server" ControlToValidate="TxtEmail" ErrorMessage="U moet een e-mail invullen als u geen telefoonnummer heeft ingevuld." Display="Dynamic" ValidationGroup="Contact"> </asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegexEmail" runat="server" ControlToValidate="TxtEmail" ErrorMessage="Dit is geen geldig e-mail adres." Display="Dynamic"  ValidationExpression="([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})" ValidationGroup="Contact"> </asp:RegularExpressionValidator>
    </td>
</tr>
<tr>
    <td>
        Telefoonnummer:
    </td>
    <td>
        <asp:TextBox ID="TxtPhone" runat="server" onchange="javascript:CheckEmailValidator(this);"></asp:TextBox>
        <asp:RequiredFieldValidator ID="ReqPhone" runat="server" ControlToValidate="TxtPhone" ErrorMessage="U moet een telefoonnummer invullen als u geen e-mail heeft ingevuld." Display="Dynamic" ValidationGroup="Contact"> </asp:RequiredFieldValidator>
    </td>
</tr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文