Hey guys i'm having a bit of trouble with a script... it's just some basic form validation but I can't get it working (I click submit and it submits even if nothing is in the text boxes).
The name of the form is "form1" and in the form tags I am putting "onsubmit="return validate_form(this);"
Here is the main code, which I am putting between the head tags:
function validemail(email)
{
invalidChars = " /:,;"
if (email == "") {
return false;
}
for (i=0; i<invalidChars.length; i++) {
badChar = invalidChars.charAt(i);
if (email.indexOf(badChar,0) > -1) {
return false;
}
}
atPos = email.indexOf("@",1);
if (atPos == -1) {
return false;
}
if (email.indexOf("@",atPos+1) > -1) {
return false;
}
periodPos = email.indexOf(".",atPos);
if (periodPos == -1) {
return false;
}
if (periodPos+3 > email.length) {
return false;
}
return true;
}
//This function will validate a form
function validateForm(form1)
{
if (form1.name.value == "")
{
alert("Please enter your name to proceed!");
form1.name.focus();
return false;
}
if (form1.comments.value == "")
{
alert("Please enter a message to proceed!");
form1.comments.focus();
return false;
}
if (!validemail(form1.email.value))
{
alert("Please enter a correct Email address!");
form1.email.focus();
return false;
}
return true;
}
Any ideas appreciated.