HelixTitan HelixTitan - 18 days ago 4
ASP.NET (C#) Question

Conditional Custom Validation on 2 TextBoxes

My goal here is to build a field that will basically make one or the other textbox required. I.E. If one textbox has some value that is not null, whitespace. or empty, I want the submit button to proceed. Else I want the standard red error to stop the submit from postingback and launching the insert function. I have tried writing this, but so far no good. What can I do to make this work?

<script type="text/javascript">
function validateText(sender, args) {
if (args.value !== "") {
var textBoxB = document.getElementById('TextBox12');
args.IsValid = (TextBox12.value !== "");
}
return;

/*
if (!string.IsNullOrEmpty(TextBox12.Text + TextBox13.Text)) {
args.IsValid = true;
}
else {
if (string.IsNullOrEmpty(TextBox12.Text) && !string.IsNullOrEmpty(TextBox13.Text)) {
args.IsValid = true;
}
else if (string.IsNullOrEmpty(TextBox13.Text) && !string.IsNullOrEmpty(TextBox12.Text)) {
args.IsValid = true;
}
else {
args.IsValid = false;
}
}
*/

}
</script>
<div>
<div class="left">
<asp:Label ID="Label13" runat="server" Text="Advanced Cancellation:"></asp:Label>
</div>
<div class="right">
<asp:TextBox ID="TextBox12" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Required"
ValidationGroup='valGroup1'
ClientValidationFunction="validateText"
OnServerValidate="ServerValidation"
ForeColor="Red"
ValidateEmptyText="true">
</asp:CustomValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ErrorMessage="Advanced Cancellation must be an integer."
ControlToValidate="TextBox12"
ValidationExpression="^\+?(0|[1-9]\d*)$"
ForeColor="Red">
</asp:RegularExpressionValidator>

</div>
</div>
<div>
<div class="left">
<asp:Label ID="Label14" runat="server" Text="Action Required (yyyy-MM-dd):"></asp:Label>
</div>
<div class="right">
<asp:TextBox ID="TextBox13" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator2" runat="server"
ValidationGroup='valGroup1'
ValidateEmptyText="true"
ErrorMessage="Required"
ClientValidationFunction="validateText"
OnServerValidate="ServerValidation"
ForeColor="Red">
</asp:CustomValidator>
<asp:CompareValidator
ID="CompareValidator1" runat="server"
Type="Date"
Operator="DataTypeCheck"
ControlToValidate="TextBox13"
ErrorMessage="Please enter a valid date."
ForeColor="Red">
</asp:CompareValidator>
</div>
</div>
protected void ServerValidation(object source, ServerValidateEventArgs args)
{
if (!string.IsNullOrEmpty(TextBox12.Text))
args.IsValid = !string.IsNullOrEmpty(TextBox13.Text);

}


That's what I have so far.

Answer

I have remove javascript code.Now your design part is

<div>
    <div class="left">
        <asp:Label ID="Label13" runat="server" Text="Advanced Cancellation:"></asp:Label>
    </div>
    <div class="right">
        <asp:TextBox ID="TextBox12" runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server"
            ErrorMessage="Required"
            ValidationGroup="valGroup1"
            OnServerValidate="ServerValidation"
            ForeColor="Red"
            ValidateEmptyText="true">
        </asp:CustomValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
            ErrorMessage="Advanced Cancellation must be an integer."
            ControlToValidate="TextBox12"
            ValidationExpression="^\+?(0|[1-9]\d*)$"
            ForeColor="Red">
        </asp:RegularExpressionValidator>

    </div>
</div>
<div>
    <div class="left">
        <asp:Label ID="Label14" runat="server" Text="Action Required (yyyy-MM-dd):"></asp:Label>
    </div>
    <div class="right">
        <asp:TextBox ID="TextBox13" runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator2" runat="server"
            ValidationGroup="valGroup1"
            ValidateEmptyText="true"
            ErrorMessage="Required"
            OnServerValidate="ServerValidation"
            ForeColor="Red">
        </asp:CustomValidator>
        <asp:CompareValidator
            ID="CompareValidator1" runat="server"
            Type="Date"
            Operator="DataTypeCheck"
            ControlToValidate="TextBox13"
            ErrorMessage="Please enter a valid date."
            ForeColor="Red">
        </asp:CompareValidator>
    </div>
</div>
    <div>
        <asp:Button ID="btnok" runat="server" ValidationGroup="valGroup1" />
    </div>

and code behind is

protected void ServerValidation(object source, ServerValidateEventArgs args)
    {
        if (!string.IsNullOrEmpty(TextBox12.Text) || !string.IsNullOrEmpty(TextBox13.Text))
            args.IsValid = true;
        else
        { 
            args.IsValid = false;
        }

    }

I have tested its work for me

Comments