English 中文(简体)
2个领域中的1个配有自动识别器
原标题:Validate 1 of the 2 fields with asp.net validators

我有一个简单的表单,其中包含一些文本框。所有这些文本框都有一个RequiredFieldValidator。如果填写了字段1,则需要禁用字段2的RequiredFieldValidator,因为只需要其中一个字段。如何完成这个任务最好? 我有一個簡單的表單,其中包含一些文字方塊。所有這些文字方塊都有一個 RequiredFieldValidator。如果填寫了字段1,則需要禁用字段2的 RequiredFieldValidator,因為只需要其中一個字段。如何完成這個任務最好?

最佳回答

目前我已经用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">&nbsp;</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">&nbsp;</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">&nbsp;</asp:RequiredFieldValidator>
    </td>
</tr>
问题回答

在这种情况下,使用服务器端验证处理程序的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))
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签