Michie
Michie

Reputation: 81

trying to fix form validation

i'll try to make form validation by giving the background colour and focus to each input when input is empty but, it turns out even though i fill the input, it still focus on the first input. It is not continue to check on the next input

I have tried with this code

    var fName=document.getElementById("in_fName");
    var lName=document.getElementById("in_lName");

//first name validation
        if (fName.innerHTML===""){
            alert("he");
            fName.style.background="#DE8971";
            fName.style.color="#FFE9D6";
            fName.focus();
        } else {
            alert("here");            
            fName.style.background="white";
            fName.style.color="black";
            return false;
        }

       //last name validation
       if (lName.innerHTML===""){
             lName.style.background="#DE8971";
             lName.style.color="#FFE9D6";
            lName.focus();
        }

Upvotes: 0

Views: 46

Answers (1)

user18529574
user18529574

Reputation:

Why not use require fields in the html section. By doing that you will not have to check for validation in JavaScript.

It would be better if you could also share your html code.

Upvotes: 0

Related Questions