[英]Either or type validation on 2 text-boxes - ASP.NET Webforms
I have two text-box fields on the Registration page and I am trying to setup either-or type of validation. 我在“注册”页面上有两个文本框字段,我正在尝试设置任一种类型的验证。 The validation should kick-in when both the fields are empty, saying you must enter either of A or B fields to be able to register.
当两个字段都为空时,验证应该启动,说您必须输入A或B字段中的任何一个才能注册。
I have used custom validator like below, in my code, but it doesn't seem to work. 我在我的代码中使用了如下所示的自定义验证器,但它似乎不起作用。 I am looking for a smart way to validate these 2 text-boxes.
我正在寻找一种智能方法来验证这两个文本框。 Server side validations part:when i tested it, the break point never hits the function.
服务器端验证部分:当我测试它时,断点永远不会到达该功能。 Can someone suggest a clean way of doing this.
有人可以建议一个干净的方式来做到这一点。
Please note that for the other controls on the page I have required field validators, I cannot have one here because I should be able to set either or condition for these 2 textboxes. 请注意,对于页面上的其他控件,我需要字段验证器,我不能在这里有一个,因为我应该能够为这两个文本框设置其中一个或条件。
<div class="form-group">
<asp:Label Text="" ID="lblSCGrantNumber" runat="server" AssociatedControlID="txtStateCommGrantNumber">
State Commission Number
</asp:Label>
<asp:TextBox ID="txtStateCommGrantNumber" runat="server" TextMode="SingleLine" placeholder="State Commission Grant Number" AutoCompleteType="None" class="form-control"></asp:TextBox>
<%-- <asp:RequiredFieldValidator Display="Dynamic" ID="rfStateCommGrantNumber" Text="Required" SetFocusOnError="true" CssClass="text-danger" ControlToValidate="txtStateCommGrantNumber" runat="server"></asp:RequiredFieldValidator>--%>
<asp:CustomValidator ID="stateCommissionGrants" runat="server" OnServerValidate="ServerValidation" OnClientValidate="Validate_textboxes" ControlToValidate="txtStateCommGrantNumber" ErrorMessage="One of the two fields is required" ValidateEmptyText="true"></asp:CustomValidator>
</div>
<div class="form-group">
<asp:Label Text="" ID="lblGrantNumber" runat="server" AssociatedControlID="txtStateCommGrantNumber">
Grant Number
</asp:Label>
<asp:TextBox ID="txtGrantNumber" runat="server" TextMode="SingleLine" placeholder="Grant Number" AutoCompleteType="None" class="form-control"></asp:TextBox>
<%--<asp:RequiredFieldValidator Display="Dynamic" ID="rfStateCommGrantNumber" Text="Required" SetFocusOnError="true" CssClass="text-danger" ControlToValidate="txtStateCommGrantNumber" runat="server"></asp:RequiredFieldValidator>--%>
<asp:CustomValidator ID="grants" runat="server" OnServerValidate="ServerValidation" OnClientValidate="Validate_textboxes" ControlToValidate="txtGrantNumber" ErrorMessage="One of the two is required" ValidateEmptyText="true"></asp:CustomValidator>
</div>
Client side code: 客户端代码:
<script type="text/javascript">
(function Validate_textboxes(sender, args) {
var v = document.getElementById('<%=txtStateCommGrantNumber.ClientID%>').value;
var v = document.getElementById('<%=txtGrantNumber.ClientID%>').value;
if (v == '') {
args.IsValid = false;
}
else {
}
});
serverside code: 服务器代码:
protected void ServerValidation(object source, ServerValidateEventArgs args)
{
args.IsValid = txtStateCommGrantNumber.Text.Trim().Length > 0 || txtGrantNumber.Text.Trim().Length > 0;
if (!args.IsValid)
{
CustomValidator customvalidator = new CustomValidator();
customvalidator.IsValid = false;
customvalidator.ErrorMessage = "TextBox1 and TexBox2 can't both be empty";
Page.Form.Controls.Add(customvalidator);
}
}
You have to use ClientValidationFunction
attribute of CustomValidator
control. 您必须使用
CustomValidator
控件的ClientValidationFunction
属性。 And within javascript function you have to set args.IsValid
property to true or false. 在javascript函数中,您必须将
args.IsValid
属性设置为true或false。 For example: 例如:
<asp:Textbox id="text1" runat="server" text=""></asp:Textbox>
<asp:Textbox id="text2" runat="server" text=""></asp:Textbox>
<asp:CustomValidator id="CustomValidator1" runat="server"
ControlToValidate = "text1"
ErrorMessage = "Required"
ClientValidationFunction="validate" >
</asp:CustomValidator>
And Javascript: 和Javascript:
<script type="text/javascript">
function validate(oSrc, args){
var v1 = document.getElementById('<%=text1.ClientID%>').value;
var v2 = document.getElementById('<%=text2.ClientID%>').value;
if (v1 == '' && v2 == '') {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
</script>
Edit: If the textbox is empty it might not trigger the validation event. 编辑:如果文本框为空,则可能不会触发验证事件。 To overcome this, you can add
ValidateEmptyText="true"
attribute to custom validator or can use separate required field validator to validate empty values. 要解决此问题,您可以将
ValidateEmptyText="true"
属性添加到自定义验证程序,或者可以使用单独的必填字段验证程序来验证空值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.