Toggle Multiple RegularExpression Validators for one TextBox based on RadioButton selection in ASP.Net

Last Reply on Sep 24, 2014 03:49 AM By Azim

Posted on Sep 24, 2014 02:27 AM

I have a RadioButtonList that includes three values: - UPC - Description - ItemCode

UPC can only allow numbers (0-9). Description can allow any text. ItemCode only allows numbers or letters.

I have a textbox (txtbox_reportBy).

Is there a way to setup three types of validators all for the same textbox based on which radiobutton value is selected?

i.e. If UPC is selected, then the textbox's validator should only allow numbers. If Description is selected, then the textbox's validator probably don't have to validate anything. If ItemCode is selected, then the textbox's validator should only allow numbers or letters.

Thanks.

Posted on Sep 24, 2014 03:49 AM Modified on on Sep 25, 2014 01:37 AM

With reference of this article.

Enable Disable ASP.Net Validator (Client Side Validation) using JavaScript or jQuery

 

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function ToggleValidator() {
            var valNumber = document.getElementById("<%=valNumbers.ClientID%>");
            var valAlphaNumeric = document.getElementById("<%=valAlphaNumeric.ClientID%>");
            var radioButton = document.getElementById("<%=rblValidationType.ClientID %>").getElementsByTagName("input");
            var selectedValue;
            for (var i = 0; i < radioButton.length; i++) {
                if (radioButton[i].checked == true) {
                    selectedValue = radioButton[i].value;
                }
            }

            switch (selectedValue) {
                case "0":
                    ValidatorEnable(valNumber, true);
                    ValidatorEnable(valAlphaNumeric, false);
                    break;
                case "1":
                    ValidatorEnable(valNumber, false);
                    ValidatorEnable(valAlphaNumeric, true);
                    break;
                case "2":
                    ValidatorEnable(valNumber, false);
                    ValidatorEnable(valAlphaNumeric, false);
                    break;

            }

            if (Page_IsValid) {
                return false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="rblValidationType" runat="server">
            <asp:ListItem Text="Numeric" Value="0" />
            <asp:ListItem Text="Alphanumeric" Value="1" />
            <asp:ListItem Text="None" Value="2" />
        </asp:RadioButtonList>
        <asp:TextBox ID="txtReportBy" runat="server" />
        &nbsp;
        <asp:Button ID="btnValidate" Text="Validate" runat="server" ValidationGroup="ValidationType"
            OnClientClick="ToggleValidator()" />
        <br />
        <asp:RegularExpressionValidator ID="valNumbers" ErrorMessage="Please enter only numbers"
            ControlToValidate="txtReportBy" runat="server" ValidationExpression="^[0-9]*$"
            Display="Dynamic" Enabled="false" ForeColor="Red" ValidationGroup="ValidationType" />
        <asp:RegularExpressionValidator ID="valAlphaNumeric" ForeColor="Red" ErrorMessage="Please enter only alphanumeric values"
            ControlToValidate="txtReportBy" runat="server" ValidationExpression="^[0-9a-zA-Z]*$"
            Enabled="false" ValidationGroup="ValidationType" Display="Dynamic" />
    </div>
    </form>
</body>
</html>

Screenshot

I agree, here is the link: http://e-iceblue.com/free-api