Validate DropDownList and display error message in JavaScript Alert Box

Last Reply on Mar 30, 2015 09:48 PM By Shaiwal

Posted on Mar 30, 2015 05:41 AM

I need help figuring out how to make alert () window not  prompt when required field is filled out. EX: When ddlFinalAction selectedIndex == 1 and TextBox5 value length > 0  then the alert () won't pop-up and user can submit data on btn_click   Thank you so much in advance!

function Validate()
            {
                 
                if (document.getElementById("<%=ddlFinalAction.ClientID%>").selectedIndex == "")
                {
                    alert('Must Enter Final Action Taken');
                    return false;
                }
                 
 
                if (document.getElementById("<%=ddlFinalAction.ClientID%>").selectedIndex == "1" || document.getElementByID"<%TextBox5.ClientID%>". value > 0)
                {
                    alert('Must Enter Comment');
                    return false;
                     
                }
 
                
                 
                return true;
           }//end validate()  

 

Posted on Mar 30, 2015 09:48 PM Modified on on Mar 30, 2015 09:49 PM

Hi @compstuden

You have done syntax erros at many places.

Refer this code

HTML

 <div>
        <asp:DropDownList ID="ddlFinalAction" runat="server">
            <asp:ListItem></asp:ListItem>
            <asp:ListItem>A</asp:ListItem>
            <asp:ListItem>B</asp:ListItem>
            <asp:ListItem>C</asp:ListItem>
        </asp:DropDownList><br /><br />
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate()" />
    </div>

Javascript

<script type="text/javascript">
        function Validate() {
            if (document.getElementById('<%=ddlFinalAction.ClientID%>').selectedIndex == 0) {
                alert('Mus Enter Final Action Taken');
                return false;
            }

            if (document.getElementById('<%=ddlFinalAction.ClientID%>').selectedIndex == 1 || document.getElementById('<%=TextBox5.ClientID%>').value.length > 0) {
                alert('Must Enter Comment');
                return false;
            }
            return true;
        }
    </script>