View Full Version : simple email validation
so..
Request.Form("name") = "" Then
noName = "yes"
-check if a name was typed in
Anyone have a quick example of how to validate email address input?
something that checks for '.' and '@'
if it finds them sets a variable to 'yes'
otherwise sets it to 'no'
angiras
12-09-2002, 08:22 PM
you must look in the direction of regular expression
it will perfectly validate your email adress
... then for a mail something like
ValidationExpression="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
I don't know what is your lscript or server langage....
with asp net you have just to use a regularExpressionVlidator control
oracleguy
12-10-2002, 12:33 AM
Try this:
Function IsValidEmail(strEmail)
Dim bIsValid
bIsValid = True
If Len(strEmail) < 5 Then
bIsValid = False
Else
If Instr(1, strEmail, " ") <> 0 Then
bIsValid = False
Else
If InStr(1, strEmail, "@", 1) < 2 Then
bIsValid = False
Else
If InStrRev(strEmail, ".") < InStr(1, strEmail, "@", 1) + 2 Then
bIsValid = False
End If
End If
End If
End If
IsValidEmail = bIsValid
End Function
whammy
12-10-2002, 04:19 AM
Or this... a much looser regular expression...
This regex has been tested on a daily basis for a couple of months now in an intranet environment where over half a dozen people go through hundreds of emails a day, and it's also in use with ASP.NET (with no problems found yet, I might add) on a colleague's email application for a while now, which makes me wonder why he's using it, does the .net email regex have a flaw? It must because he wouldn't have asked for a better regex otherwise... I suppose I should pose that question to him myself tomorrow! ;)
FYI, most more "strict" regex's that I've tried usually don't allow perfectly valid email addresses sometimes - for instance the first one posted here would fail against a valid email address like John+O'Connor@whatever.net ;):
Function ValidEmail(str)
Dim veRegEx
Set veRegEx = New RegExp
veRegEx.Pattern = "^[\w\+\'\.-]+@[\w\'\.-]+\.[a-zA-Z]{2,4}$"
ValidEmail = veRegEx.Test(str)
End Function
Hope this helps... it may not be perfect, and no email validation scheme is, but it's the best compromise I've found yet between being TOO strict and still making sure people enter an email address in the correct basic format.
Unless you actually send an email and see if it bounces, or at least use a "sniffer" program to see if the domain/email exists, this is probably the best way to go... of course you can use this in conjunction with such programs to make sure the email is of a basic valid format to begin with before you sniff or whatever, therefore saving resources. :)
whammy
12-10-2002, 04:54 AM
P.S. Instead of setting a variable to "yes" or "no" it's a good programming practice to use boolean values (True or False) instead (you will see that in constant use on the javascript forum!).
For instance you'd check an email with the function above like so (in ASP):
EMail = "blah@blah.com"
If ValidEmail(EMail) = True Then
'Yay! The email is good!
Else
'Boo! The email is very bad!
End If
The function itself returns a boolean (True or False) value. It's a good thing to get familiar with since that's how many (if not all) programming languages and functions work.
P.S.
Getting into the habit of using strings like "yes" or "no" as a variable depending upon a condition is a very, very bad habit... from some code I've seen, you'll likely end up writing code like:
IF ((emailbad <> "yes" AND email <> "") AND _
(namebad <> "yes" AND name <> "")) OR _
((asdfbad <> "yes" AND asdf <> "") AND _
(qwerbad = "no" OR foobar <> "yes")) THEN
Which will not only be confusing to you in the long run, but will have to be rewritten by other developers if they ever need to update your application (let alone understand it!). :)
hoho :D !
cheers. :thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.