My form’s validate() is not working

You should use document.getElementsByName if you want to fetch the value using name attribute. I would recommend to do it by “id” attribute.

function submitFunction() {
        var firstName = document.getElementsByName("first_Name")[0].value;
         if (firstName == "" || firstName == null || firstName.length == 0) {
            alert("First Name is required");
            return false;
        } 
    }

Also add return to the onsubmit event to avoid page refresh.

<form name="Register" onsubmit="return submitFunction() " method="post">

Option 2 : Using HTML5

The required attribute will do the validation check and you can skip javascript.

<input type="text" name="first_Name" required>

Leave a Comment