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

So, recently I came across a problem where I had wanted to invoke asp.net required filed from JavaScript. Fortunately, there is a way - and it’s pretty easy to do! Let’s take a look.
In this post, I will show you how to invoke the required field validator through javascript 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>

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post