Jump to content

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"

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.