How to do basis validation using Javascript in asp.net ?


Hi

So many time, we will get scenario to do validation work or some other stuff using JavaScript in asp.net. Here is the basic validation syntax using JavaScript in asp.net.

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

<!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 id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
                function doValidation() {
                    var name = document.getElementById("<%=txtEmpName.ClientID%>").value;
                    var empSal = document.getElementById("<%=txtEmpSal.ClientID%>").value;
                    var empAddress = document.getElementById("<%=txtEmpAddress.ClientID%>").value;
                    if (name == "") {
                        alert("Please enter the EmpName");
                    }
                    else if (empSal == "") {
                        alert("Please enter the EmpSal");
                    }
                    else if (isNaN(empSal)) {
                        alert("Invalid EmpSal");
                    }
                    else if (empAddress == "") {
                        alert("Please enter the EmpAddress");
                    }

                }
                
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        EmpName:
        <asp:TextBox ID="txtEmpName" runat="server" />
        <br />
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp; EmpSal
        <asp:TextBox ID="txtEmpSal" runat="server" />
        <br />
        <br />
        EmpAddress
        <asp:TextBox ID="txtEmpAddress" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnClick" runat="server" OnClientClick="doValidation();return false;"
            Text="Submit" />
    </div>
    </form>
</body>
</html>

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.