Jump to content

Need JavaScript Help


Err
 Share

Recommended Posts

I'm doing this e-mail validation thing in JavaScript, and I'm getting stuck on the match() code (bold in the code). It's suppose to catch when someone puts in slashes, semi-colons, etc. and basically anything that's not allowed in a e-mail address and displays an alert box telling the user. I'm obviously doing something wrong because it's not working. I've tried for one hour trying to get it to work, I'm sure it's something simple, but I just can't see where I'm going wrong. Please look at my code and tell me if you guys see anything:

<script type="text/javascript"><!--function validate_email(field,alerttxt){  with (field)  {    apos=value.indexOf("@")    dotpos=value.lastIndexOf(".")    if (apos<1||dotpos-apos<2) {      alert(alerttxt);      return false;    }    else {      return true;    }  }  }[b]function validate_email_char(field,alerttxt){  with (field)  {    illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;          if (field.match(illegalChars)) {      alert(alerttxt);      return false;    }    else {      return true;    }  }}[/b]function validate_form(thisform){  with (thisform)  {        var getid = document.getElementById("contact_form");        if (getid.email.value=="")    {      alert("Please fill in your e-mail address.");      email.focus();      return false;    }    else if (validate_email(email,"Not a valid e-mail address.")==false)    {      email.focus();      email.select();      return false;    }    else if (validate_email_char(email,"E-mail contains illegal characters."))    {      email.focus();      email.select();      return false;    }  }}// --></script><form id="contact_form" action="" onsubmit="return validate_form(this);">  <div>E-mail: <input type="text" name="email" /></div>  <div><input type="submit" value="Submit" /></div></form>

Here's a link to the page: http://oneuse.awardspace.com/test/test_f1.html

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>  <title>E-mail Validation</title>  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1;" />  <meta name="robots" content="noindex, nofollow" />  <link rel="icon" type="image/x-icon" href="/err.ico" />  <link rel="shortcut icon" type="image/x-icon" href="/err.ico" /><style type="text/css">form {  border: 1px red solid;}</style><script type="text/javascript"><!--function validate_email(field,alerttxt){  with (field)  {	apos=value.indexOf("@");	dotpos=value.lastIndexOf(".");	if (apos<1||dotpos-apos<2) {	  alert(alerttxt);	  return false;	}	else {	  return true;	}  }  }function validate_email_char(field,alerttxt){  with (field)  {	illegalChars=/[\(\)\<\>\,\;\:\\\/\"\[\]]/;	  	if (field.value.match(illegalChars)) { /* Oops, your fault error was here */ 	  alert(alerttxt);	  return false;	}	else {	  return true;	}  }}function validate_form(thisform){  with (thisform)  {		var getid = document.getElementById("contact_form");		if (getid.email.value=="")	{	  alert("Please fill in your e-mail address.");	  email.focus();	  return false;	}	else if (validate_email(email,"Not a valid e-mail address.")==false)	{	  email.focus();	  email.select();	  return false;	}	else if (validate_email_char(email,"E-mail contains illegal characters."))	{	  email.focus();	  email.select();	  return false;	}  }}// --></script></head><body><form id="contact_form" action="" onsubmit="return validate_form(this);">  <div>E-mail: <input type="text" name="email" value="ex;a/m\ple@exp.com" /></div>  <div><input type="submit" value="Submit" /></div></form></body></html><!-- Last Edited: 9/18/2007 3:37:15 PM PM -->

There, fixed it. It works fine for me, it displays all 3 kinds of errors now. match() only operates on variables with string values, which field itself was not a string value, but instead the identifier for the form.

Link to comment
Share on other sites

Thanks a lot! That did it for me also. I knew it was something I was over looking, it turned out to be something small, lol .value

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...