Do you want the color to be red if the the field contains incorrect data and in all other cases it should be white?
If so, why not remove the
else if
and change it to
else
?
if(regCompanyname.test(companyname) == false) {
document.getElementById("companyname").style.backgroundColor="#FBBBB9";
} else {
document.getElementById("companyname").style.backgroundColor="#FFFFFF";
}
[UPDATE]
Try this approach instead or a variant there of.
if (companyname == '') {
document.getElementById("companyname").style.backgroundColor="#FFFFFF";
} else if(regCompanyname.test(companyname) == false) {
document.getElementById("companyname").style.backgroundColor="#FBBBB9";
} else {
document.getElementById("companyname").style.backgroundColor="#FFFFFF";
}