Skip to main content

How to Invoke required field validator through javascript in asp.net

In this post I will show you how to invoke required field validator through java script in asp.net.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReqJs.aspx.cs" Inherits="ReqJs" %>

<!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 PerformCheck() {
            if (Page_ClientValidate()) {
                //Do some more stuff here
                alert("Page Is Valid");
            }

        }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        FirstName:
        <asp:TextBox runat="server" ID="txtFirstName"></asp:TextBox>
        <asp:RequiredFieldValidator ErrorMessage="*" Text="Please Enter first name" ControlToValidate="txtFirstName"
            runat="server" /><br />
        LastName:
        <asp:TextBox runat="server" ID="txtLastName"></asp:TextBox>
        <asp:RequiredFieldValidator ErrorMessage="*" Text="Please enter last name" ControlToValidate="txtLastName"
            runat="server" /><br />
        <input type="submit" id="btnSubmit" onclick="PerformCheck();" value="Validate" />
    </div>
    </form>
</body>
</html>

Comments

Popular posts from this blog