Validate DropDownList using JavaScript and Custom Validator in ASP.Net

Last Reply on Mar 01, 2014 07:48 AM By Azim

Posted on Mar 01, 2014 12:15 AM

Hi sir Goodmorning

 

M actually using lot of dropdownlist in a form and i want to validate all the dropdown lists using java script. I know it can be done using required field validator  asp.net control. But if i use this

my alignment changes. So i would like to go for javascript and display an (*) .

Please help me out sir.

Posted on Mar 01, 2014 07:48 AM

You can set Display Dynamic for ValidationControl.

HTML:

<form id="form1" runat="server">
    <div>
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    Education
                </td>
                <td>
                    <asp:DropDownList ID="ddlEducation" runat="server">
                        <asp:ListItem Text="PleaseSelect" Value="0" />
                        <asp:ListItem Text="BE" Value="1" />
                        <asp:ListItem Text="BSCIT" Value="2" />
                        <asp:ListItem Text="Diploma" Value="3" />
                        <asp:ListItem Text="HSC" Value="4" />
                    </asp:DropDownList>
                    <asp:CustomValidator ID="cvEducation" runat="server" Display="Dynamic" ForeColor="Red"
                        ErrorMessage="*" ControlToValidate="ddlEducation" ClientValidationFunction="EducationCheck"></asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td>
                    Board
                </td>
                <td>
                    <asp:DropDownList ID="ddlBoard" runat="server">
                        <asp:ListItem Text="PleaseSelect" Value="0" />
                        <asp:ListItem Text="MSBTE" Value="1" />
                        <asp:ListItem Text="MUMBAI" Value="2" />
                        <asp:ListItem Text="DELHI" Value="3" />
                        <asp:ListItem Text="VTU" Value="4" />
                    </asp:DropDownList>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" Display="Dynamic" ForeColor="Red"
                        ErrorMessage="*" ControlToValidate="ddlBoard" ClientValidationFunction="BoardCheck"></asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button Text="Save" runat="server" />
                </td>
            </tr>
        </table>
    </div>
    </form>

Java Script:

  <script type="text/javascript">
        function EducationCheck(source, args) {
            var ddlEducation = document.getElementById("<%=ddlEducation.ClientID%>");
            var country = ddlEducation.options[ddlEducation.selectedIndex].value;
            if (country == "0") {
                args.IsValid = false;
            }
            else {
                args.IsValid = true;
            }
        }
        function BoardCheck(source, args) {
            var ddlEducation = document.getElementById("<%=ddlBoard.ClientID%>");

            var country = ddlEducation.options[ddlEducation.selectedIndex].value;
            if (country == "0") {
                args.IsValid = false;
            }
            else {
                args.IsValid = true;
            }
        }
    </script>

Thank You.

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html