Jump to content

Basic form validation


jeeves245

Recommended Posts

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. :)

 

Link to comment
https://forums.phpfreaks.com/topic/178414-basic-form-validation/
Share on other sites

Hmm...

 

You're calling the function like so

onsubmit="return validate_form(this);"

 

And, here is the function

function validateForm(form1)
{
    //bunch of validation code
}

 

I couldn't imagine what could be wrong.

 

Ok, I was just being sarcastic, it happens to the best of us. But, if you still don't see the problem:

 

"validate_form" != "validateForm"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.