Friday, 20 July 2012

Validate(value Required) Text Box Using Javascript in asp.net


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

<!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></title>
    <script type="text/javascript">
        function Myfunc() {
            if (check("<%=TextBox1.ClientID %>"))
                alert("Fill TextBox1");
            else if (check("<%=TextBox2.ClientID %>"))
                alert("Fill TextBox2");
            else if (check("<%=TextBox3.ClientID %>"))
                alert("Fill TextBox3");
            else if (check("<%=TextBox4.ClientID %>"))
                alert("Fill TextBox4");
            else if (check("<%=TextBox5.ClientID %>"))
                alert("Fill TextBox5");
        }
        function check(Id) {
            if (!document.getElementById(Id)) return false;
            return (document.getElementById(Id).value.length == 0)
        }
 </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    <center>
       <asp:Label runat="server" ID="Label1" Text="TextBox Validation using Javascript" ForeColor="Red"></asp:Label><br />
    <asp:Label runat="server" ID="Lable1" Text="Lable1"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:Label runat="server" ID="Label2" Text="Lable2"></asp:Label>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    <asp:Label runat="server" ID="Label3" Text="Lable3"></asp:Label>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <br />
    <asp:Label runat="server" ID="Label4" Text="Lable4"></asp:Label>
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <br />
    <asp:Label runat="server" ID="Label5" Text="Lable5"></asp:Label>
    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Myfunc();" />
    </center>
    </form>
</body>
</html>

No comments:

Post a Comment