jQuery Empty Field Validation Example

<html>
<head>
    <title>Textbox</title>
    <script type="text/javascript" src="jquery.js"></script>
    <script>
        $(document).ready(function(){
            $("#button").click(function(){
                if($("#username").val()==''){
                    document.getElementById("uname").innerHTML="Username Required";
                    return false;
                }
                if($("#password").val()==''){
                    document.getElementById("pass").innerHTML="Password Required";
                    return false;
                }
            });
        });
    </script>
</head>
<body>

<form id="form" action="jquery.js" method="POST">
    <input type="text" id="username" placeholder="Enter Username"><b id="uname"></b><br>
    <input type="password" id="password" placeholder="Enter Password"><b id="pass"></b><br>
    <input type="submit" id="button" value="Button">
</form>

</body>
</html>

Comments