Saturday, 5 April 2014

To Check Empty TextBox and DropDownList using JavaScript Validation in Asp.Net

Ans:-
Here i have taken two TextBox and one DropDownList to check TextBoxes and DDL is Empty or not.If Empty....it will show message to Enter the Required field......See the below Codes..

validationusingJS.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="validationusingJS.aspx.cs" Inherits="validationusingJS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Validation using JS</title>
    <script type="text/javascript" language="javascript">
        function validate() {
            var id = document.getElementById("txtId");
            var name = document.getElementById("txtName");
            var items=document.getElementById('<%=DropDownList1.ClientID%>')
            if (id.value == "") {
                id.focus();
                alert("Plz Enter Id..");
                return false;
            }
            else if (name.value == "") {
                name.focus();
                alert("Plz Enter Name..");
                return false;
            }
            else if (items.selectedIndex == 0) {
            items.focus();
            alert("Plz Enter Items..");
            return false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        ID : <asp:TextBox ID="txtId" runat="server"></asp:TextBox>
        <br />
        Name : <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <br />
        Select Items : <asp:DropDownList ID="DropDownList1" runat="server">
         <asp:ListItem Text="Item1" Value="Item1"></asp:ListItem>
        <asp:ListItem Text="Item2" Value="Item2"></asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:Button ID="btnSubmit" OnClientClick="return validate()" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>



No comments:

Post a Comment