Monday, September 10, 2018

C # client validation

Validation with two text boxes for sample purpose.

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function validate() {
            var x = document.getElementById("<%=txtName.ClientID%>").value;
            var y=document.getElementById("<%=txtCity.ClientID%>").value;
            if (x == "") {
                alert("Please enter Name");
            }
            if (y == "") {
                alert("Please enter City");
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        Name<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <br />
        City
        <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btnAdd" runat="server" Text="Add" OnClientClick="return validate();"/>
 
    </div>
    </form>
</body>
</html>

No comments: