This javascript form validation won’t go through even when the fields have been filled in. Any advice?

<script type="text/javascript">
function checkforinput() {

if (document.getElementById('fname').value == ""){
document.getElementById('fname').style.borderColor = "red";
document.getElementById('errMsg').innerHTML = "*Please enter First and Last Name";
}    
else {
document.getElementById('fname').style.borderColor = "#c5c5c5";
document.getElementById('errMsg').innerHTML = "";
}

if (document.getElementById('lname').value == ""){
document.getElementById('lname').style.borderColor = "red";
document.getElementById('errMsg2').innerHTML = "*Please enter First and Last Name";
}
else {
document.getElementById('lname').style.borderColor = "#c5c5c5";
document.getElementById('errMsg2').innerHTML = "";
}

if (document.getElementById('email').value == ""){
document.getElementById('email').style.borderColor = "red";
document.getElementById('errMsg3').innerHTML = "*Please enter a valid email";
}
else {
document.getElementById('email').style.borderColor = "#c5c5c5";
document.getElementById('errMsg3').innerHTML = "";
}


return false;
}
</script>

Hey guys. So my question is regarding this chunk of Javascript code. So these inputs (fname, lname, and email) have a span right next to them with the id errMsg that will display whatever the value of .innerHTML is. The validation works. Except whenever I fill in all the fields completely, my submit button won’t process the form action i have in place. Nothing happens. Any ideas as to why this is happening. I feel it has to do something with the ELSE in the conditional statement but i don’t know what it is exactly. The reason I use ELSE here, is because it turns the field back to how it looked like before. But maybe its actually screwing me over here. I need help guys. Any advice is appreciated.
This is my HTML code:

<form name="postform" action="postingprocessed.php" method="post" onsubmit="return checkforinput()">
<div class="fullName">
<input id="fname" type="text" maxlength="100" placeholder="First Name" name="first_name" /><span id="errMsg"></span>
<input id="lname" type="text" maxlength="100" placeholder="Last Name" name="last_name" /><span id="errMsg2"></span> 
</div>
<input id="email" class="text" type="email" maxlength="100" placeholder="Email" name="email"/><span id="errMsg3"></span><br>    
<input id="submitBtn" class="submitBtn" type="submit" value="Post This!" name="SubmitBtn"/>
</form>    


Source: html

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.