Hello, I'm trying to validate the form, but each time I try to do so, all fields return invalid. I've tested my regular expressions with various online testers and they seem to work fine, am I perhaps using it in the wrong way? Or maybe a problem with my functions?:-/

Any Help would be very much appreciated.

Here's my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Nicholas Gingerella Assignment 7</title>
</head>
<body>
<script language="javascript" type="text/javascript">
function validate_email()
{
  if (document.FORM.email.value==/^\w+@\w+\.com|net|edu$/)
  {;return true;}
  else if (document.FORM.email.value==/^\w+\.\w+@\w+\.com|net|edu$/)
  {;return true;}
  else 
  {alert("Not a valid email address");return false;}
}

function validate_fullname()
{
  if (document.FORM.fullname.value==/^[a-zA-Z]+\s[a-zA-Z]+$/)
  {;return true;}
  else 
  {alert("Invalid name format.");return false;}
}


function validate_zipcode()
{
  if (document.FORM.zipcode.value==/^[0-9]{5}$/|/^[0-9]{5}-[0-9]{4}$/)
  {;return true;}
  else 
  {alert("Invalid zipcode format.");return false;}
}

function validate_phonenumber()
{
  if (document.FORM.phone.value==/^[0-9]{3}-[0-9]{4}$/|/^[0-9]{3}-[0-9]{5}-[0-9]{4}$/)
  {;return true;}
  else 
  {alert("Not a valid phone number.");return false;}
}

function validate_form()
{
  if (validate_fullname()==false|validate_email()==false|validate_zipcode()==false|validate_phonenumber()==false)
    {return false;}
	else
	{return true;}
}
</script>
<form name="FORM"  onsubmit="return validate_form()">
Enter the requested information below:
<br />
First and last name:
<input type="text" name="fullname" />
<br />
Email Address:
<input type="text" name="email" />
<br />
Zip Code:
<input type="text" name="zipcode"  />
<br />
Phone number:
<input type="text" name="phone"  />
<br />
<input type="submit" value="Submit"/>
</form>
</body>
</html>

Recommended Answers

All 3 Replies

if (validate_fullname()==false|validate_email()==false|validate_zipcode()==false|validate_phonenumber()==false)
    {return false;}
	else
	{return true;}
}

You want the .test method (not an == comparison). Individual tests should look something like these

var emailRE=/^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
  if (!emailRE.test(theForm.FieldData1.value))  { //1=Email
    alert("The 'Email' is not valid.");
    theForm.FieldData1.focus();
    return (false);
  }

  var phoneRE=/^(\d{3}-){1,2}\d{4}$/
  if (!phoneRE.test(theForm.FieldData2.value)) { //2=Phone
    alert("The 'Phone' field is is not valid.");
    theForm.FieldData2.focus();
    return (false);
  }

and if you group them in a function which ends with return true; the messy boolean of booleans quoted above won't be needed.

Member Avatar for rajarajan2017
function validate_form()
{
	var testVar=/^[a-zA-Z]+\s[a-zA-Z]+$/
	if (!testVar.test(document.FORM.fullname.value))
	{
		alert("Invalid name format.");
		return (false);
	}

	testVar=/^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/  
	if (!testVar.test(document.FORM.email.value)) 
	{
		 alert("The 'Email' is not valid.");    
		 document.FORM.email.focus();    
		 return (false); 
	}
}

Just form the structure like this and call the validate_form as it is

I see, thank you very much fxm and rajarajan07, this helped me a lot. I'm still not used to web programming yet, lol, thanks for the help guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.