html form with javascript validation code example

Example 1: javascript login validation

<script language = "JavaScript">
        function validate() {
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            if (username == null || username == "") {
                alert("Please enter the username.");
                return false;
            }
            if (password == null || password == "") {
                alert("Please enter the password.");
                return false;
            }
            alert('Login successful');
            
        } 
</script>

Example 2: javascript form validation

<form name="myForm" action="/action_page.php" onsubmit="return validateForm()" 
method="post">

 Name: <input type="text" name="fname">

<input type="submit" value="Submit">

</form>