Forum Moderators: coopster & phranque

Message Too Old, No Replies

Quick Tip: Email address validation in Perl

         

sugarkane

10:06 pm on Feb 27, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While it's impossible to check that an email address supplied through a form is actually correct, it's nice to confirm that it's at least in the correct format. The following Perl snippit will do this:

if ($email_address =~ /^(\w¦\-¦\_¦\.)+\@((\w¦\-¦\_)+\.)+[a-zA-Z]{2,}$/)
{
print "$email_address is valid";
}
else {
print "$email_address is invalid";
}

How it works:

It uses a 'regular expression' to check that the address supplied conforms to the desired format. Regular expressions (also known as regex) are a *very* powerful and useful way of matching patterns in Perl, PHP and many other languages.

The first part of the regex ^(\w¦\-¦\_¦\.)+\@ checks that the address begins with any combination if alphanumeric characters, dashes, underscores or dots any number of times, followed by an @

((\w¦\-¦\_)+\.) deals with subdomains, checking for a group of any number of valid characters followed by a dot. The + sign after that allows for any number of subdomains.

Finally, [a-zA-Z]{2,3}$ checks that the address ends in at least 2 alpha characters to cope with the top level domain.

I can't guarantee that this snippit will work with every single email address you pass through it as it doesn't strictly follow the RFC for email addresses, but I use it extensively and have not come across a problem yet. Any suggestions for improvement are very welcome!

(note: comes in very handy for gently reminding AOL users that their screen name is *not* their email address!)

mivox

1:01 am on Feb 28, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's very cool... will have to remember this one. On of the less elegant methods I use is to send an autoresponse back to the form submitter... if I get a bounce, I don't do anything with their form input.

>reminding AOL users that their screen name is *not* their email address!

Good lord, that actually happens? *shudder*

Brett_Tabke

8:13 pm on Feb 28, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Oh - it happens. I get 5-10 emails a day from people on one site without the @domain.com part. Sometimes those are Aol'ers, and other times it is Webtv'ers.

Thanks for the snip of code Sugarkane. Already used it once.

Xoc

11:48 am on Mar 1, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Be warned that the script will break when the new top level domains hit the street. The new ones will have longer names (info, museum), and the script given will reject all top level domains that don't have two or three characters in them. Plan to revisit the script when the new domains become available.

NFFC

11:59 am on Mar 1, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This one seems to do the job in ASP:

 <%@ LANGUAGE="VBSCRIPT" %>

<%
Form_Email = Trim(Replace(Request.Form("email"),"""",""""""))
Validated_Form = true
IF len(Form_Email)<6 OR InStr(Form_Email,"@")=0 THEN
Validated_Form = false
END IF
IF len(Form_Email)<6 OR InStr(Form_Email,".")=0 THEN
Validated_Form = false
END IF
IF NOT Validated_Form THEN
%>

Some HTML


else

blah blah

sugarkane

12:35 pm on Mar 1, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>Be warned that the script will break when the new top level domains hit the street.

Ack! The actual snippet will work (I spotted that bug just before I posted and fixed it), but I forgot to change the relevant part of the explanation.

"Finally, [a-zA-Z]{2,3}$ checks that the address ends in at least 2 alpha..." should of course be "Finally, [a-zA-Z]{2,}$ checks that the address ends in at least 2 alpha..."

sugarkane

12:39 pm on Mar 1, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>This one seems to do the job in ASP:

I don't know vbscript, but this seems to just check that the address is at least 6 characters long and contains both a dot and an @

Am I reading this right?

BoneHeadicus

4:10 pm on Mar 1, 2001 (gmt 0)

10+ Year Member



While we're on this subject I found a snippet that does this client side.

In the head
<script language="JavaScript">
<!--
tmt_regExpValidator(f,re,eMsg,ru,r){
var myErr="";var fv=MM_findObj(f).value;var rex=new RegExp(unescape(re));
var t=eval(ru+rex.test(fv));if(r){if (fv.length<=0¦¦!t){alert(unescape(eMsg));myErr += 'eMsg';}}
else if(fv.length>0&&!t){alert(unescape(eMsg));myErr += 'eMsg';}document.MM_returnValue=(myErr=="");
}
//-->
</script>

Then down in the form

<input type="text" name="Email:" size="26" maxlength="79" onChange="tmt_regExpValidator('Email:','%5E%5B%5Cw%5C. =-%5D+@%5B%5Cw%5C.-%5D+ %5C.%5Ba-z%5D%7B2,3%7D$', 'Invalid%20data.%20Should%20be%20in%20this%20type%20format%20%27you @ you.com%27','','1'); return document.MM_returnValue">

This coupled with sugarkanes formmail hack to get the address off the page makes a winning combination for me.

This is a plug in that writes this for me.You can get the regex expression extension above at Macromedia Exchange. It does all kinds of other form verification as well.

sugarkane

7:00 pm on Mar 3, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For PHP fans, this does the same thing:

<?
if (ereg("^(\w¦\-¦\_¦\.)+\@((\w¦\-¦\_)+\.)+[a-zA-Z]{2,}$", $email_address)) {
echo("$email_address is valid<br>");
}
else {
echo("$email_address is not valid<br>");
}
?>

Added: For some reason, when I copy and paste code, something gets lost in the translation. I'm not sure if it's my browser, editor or whatever. If you're having trouble using the snippets, make sure that the 'pipe' character ¦ is actually a pipe character when you've pasted.

Xoc

11:46 pm on Mar 4, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On ASP, I use a bit of server-side JavaScript that I stole and modified from here: [javascript.internet.com...]

Incidentally, if you are doing JavaScript, the library at [javascript.internet.com...] is pretty cool.