I have to build a small contactform validator in JS, which checks the fields' values when submit is clicked. I am experiencing problems with getting the email validation to work properly. This is what I've got:
function validate_form()
{
valid = true;
// Validation of email input
if ( (document.contact.email.length < 7) || (document.contact.email.value == "") || (!document.contact.email.indexOf("@")) || (!document.contact.email.indexOf(".")) )
{
alert("Please fill in a correct email address.");
valid = false;
}
return valid;
}
When I don't fill in anything in the email field, I indeed get the proper alert. But when I fill in a string between 1 and 7 characters, the (document.contact.email.length < 7) is not met. The same goes for the last two conditions: (!document.contact.email.indexOf("@")) || (!document.contact.email.indexOf(".")). Even when (one of) these two conditions are not met, it is possible to submit the form...
Does anybody know what I am doing wrong? I am new with developing and especially with JS so please go a little bit easy on me. :)
Thanks!