44 thoughts on “#26 Form Validations in JavaScript

  1. I figured out an alternative which doesn't use return in Js function. Code below:

    function validate() {

    var uname = document.getElementsByTagName("input")[0].value.trim();

    var pass = document.getElementsByTagName("input")[1].value.trim();

    if(uname.length == 0 || pass.length == 0) {

    alert("either of the input value is blank!");


    else {

    document.getElementsByTagName("form")[0].action = "welcome.html";



  2. Hi, Thanks for the video it is very helpful and i am trying to execute this form tag and JS file, but JS validation is not working for me. but it is redirecting to another page with welcome message. it is not checking the blank field. can you pls assist me on this?

  3. THANK YOU!! I've been looking at tutorials for HOURS and this is the only one that was complete, easy to understand, and that actually worked. You saved me so much time and headache.

  4. this is great tutorial but i put every detail in my code editor sublime text3 unfortunately it doesn't work i think people comment here they're not try to use this code thats why they say this is great code.

  5. There is minute modification in the code. It might help
    <!DOCTYPE html>

    <html lang="en">


    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Login Page</title>

    <script type="text/javascript">

    function Validate()


    var username= document.getElementById("user").value;

    var password= document.getElementById("pass").value;

    if((username.trim()=='')||(password .trim()==' '))


    alert("The field is empty");

    return false;




    return true;






    <form onsubmit="return Validate()" action="Welcome.html">

    <input type="text" id="user" placeholder="Username"><br><br>

    <input type="password" id="pass" placeholder="Password"><br><br>

    <button type="submit">Submit</button>




Leave a Reply

Your email address will not be published. Required fields are marked *