required field validator in asp.net for textbox using javascript

In this article, we are valid the textbox for required field using java script. Java script is a client side scripting language that means it check the textbox on client side not the server side.
Use this script code, we will validate the required filed of the textbox.

Script code :-
    <script language="javascript" type="text/javascript">
        function requiredvalidator(valueoftextbox) {
            var TextBox1 = valueoftextbox.value;
            var result = document.getElementById("lbresult");
            if (TextBox1 == "") {
                result.style.display = "";
                return false;
            }
            else {
                result.style.display = "none";
            }
            return true;
        }
    </script>

Source code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="validator.aspx.cs" Inherits="validator" %>

<!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 language="javascript" type="text/javascript">
        function requiredvalidator(valueoftextbox) {
            var TextBox1 = valueoftextbox.value;
            var result = document.getElementById("lbresult");
            if (TextBox1 == "") {
                result.style.display = "";
                return false;
            }
            else {
                result.style.display = "none";
            }
            return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" onblur="requiredvalidator(this);"></asp:TextBox>
        <asp:Label ID="lbresult" runat="server" Style="display: none" Text="Required Field"></asp:Label>
    </div>
    </form>
</body>
</html>


Out Put:-



required field validator in asp.net for textbox using javascript required field validator in asp.net for textbox using javascript Reviewed by NEERAJ SRIVASTAVA on 9:30:00 PM Rating: 5

No comments:

Powered by Blogger.