if ($email_address =~ /^(\w¦\-¦\_¦\.)+\@((\w¦\-¦\_)+\.)+[a-zA-Z]{2,}$/)
{
print "$email_address is valid";
}
else {
print "$email_address is invalid";
}
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!)
>reminding AOL users that their screen name is *not* their email address!
Good lord, that actually happens? *shudder*
<%@ 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
elseblah blah
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..."
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.
<?
if (ereg("^(\w¦\-¦\_¦\.)+\@((\w¦\-¦\_)+\.)+[a-zA-Z]{2,}$", $email_address)) {
echo("$email_address is valid<br>");
}
else {
echo("$email_address is not valid<br>");
}
?>
Incidentally, if you are doing JavaScript, the library at [javascript.internet.com...] is pretty cool.