I'm facing an issue with this code. If I uncomment the specific line, the form bypasses validation and goes directly to the linked page in the action attribute. However, if I keep it commented out, the validation runs smoothly, and the alert box displays the appropriate message. I'm puzzled by why this discrepancy is occurring.
function validateForm(){
var flag=0;
var username=document.forms["f1"]["uname"].value;
var password=document.forms["f1"]["pass"].value;
var firstName=document.forms["f1"]["fname"].value;
var lastName=document.forms["f1"]["lname"].value;
var phoneNumber=document.forms["f1"]["phone"].value;
var emailAddress=document.forms["f1"]["email"].value;
var error="";
if(username==""||username==null) {
error+="Username cannot be left blank\n";
//document.getElementsById("uname").style.backgroundColor="red";
flag=1;
}
if(password==""||password==null){
error+="Password cannot be left blank\n";
flag=1;
}
if(emailAddress==""||emailAddress==null){
error+="Email cannot be left blank\n";
flag=1;
}
if(flag==0){
return true;
}else{
alert(error);
return false;
}
}