• Home
  • About
  • Doom II
  • Flog
  • Inspiration

Web Form Validation For E-Mail

Posted in Computing. on Thursday, March 13th, 2008 by Derek
Mar 13

Ever come across a form prompting for your e-mail address only to find out it won’t accept your perfectly legitimate handle, like romeo+juliet@shakespeare.ca? Theoretically you can also send mail to a user at an IP address instead of a domain name… so why are these false positives being thrown? Maybe it’s because the pattern to recognize a proper address is a complete retarded regular expression. When you factor in the escape characters of a programming language, who would want to re-implement this? Not to mention, there will soon no longer be a Latin alphabet stranglehold on e-mail addresses.

At this point, the most simple method is to check for a single @ sign with characters before and after. Perform the check in the web browser and also on server-side just in case JavaScript is disabled. In Javascript, the function will just look like this:

function validateForm(form)
{
	if(!/^[^@]+@[^@]+$/.test(form.email.value.replace(/^\s+|\s+$/g, '')))
	{
		alert("No, just no.");
		form.email.focus();
		return false;
	}
	else
	{
		return true;
	}
}
<form ... onsubmit="return validateForm(this)">
...
	<input name="email" type="text" />
...
</form>

So why verify? You may break XHTML compliancy on <a href=”mailto:… tags? Prevent sending messages to multiple recipients by throwing a comma or semicolon in the input text field? Why are you even collecting the e-mail address in the first place? What will you use it for? Registration? I think I validate just as a matter of principal (also known as OCD) to ensure every bit in my database has integrity. It’s to the point where you really should just verify the address by attempting to fire off a message containing a clickable link to verify a hash saved to the database. Let your mailing API throw an exception if the address is invalid. Of course, when spam filters just kill the email, what’s the point?

This is where we have a web development existential crisis.

  • Now Playing: Hybrid - Wide Angle - 06 - Snyper

3 Comments

  1. Jon on March 14th, 2008

    It’s both a blessing and a curse that web forms often don’t accept ‘+’ as part of an email address, in my opinion.

  2. Afterglow on March 16th, 2008

    That’s just so you can avoid receiving spam, you sly Limey bastard.

  3. Jon on March 17th, 2008

    How did you guess?!?



Leave a Reply

CAPTCHA Image
Refresh Image
*

Derek MacDonald


  • Photo Stream
  • Categories
    • Australia
    • Computing
    • Film & TV
    • Food
    • Gaming
    • General
    • Music
    • Sports
    • Visual Art
  • Search






  • Home
  • About
  • Doom II
  • Flog
  • Inspiration

© Copyright Derek MacDonald. All rights reserved.
Designed by FTL Wordpress Themes brought to you by Smashing Magazine

Back to Top