PDA

View Full Version : Validating an email address.


Mhtml
10-10-2002, 05:37 AM
I can validate a users email address for the @ symbol and for having anything entered at all but I would like to know if I can some how check for words before and after the @ and for at least 1 "." after the word which is after the @ ...

Does this even make sense?

whammy
10-10-2002, 05:48 AM
If you want to get more robust, check out regular expressions... they can be as simple or as complicated as you want... although I just go minimal, since even if you check for valid things, people can always fool your RegEx...:


Function ValidEmail(email) ''''''''''''''''''''''
Dim veRegEx
Set veRegEx = New RegExp
veRegEx.Pattern = "^[\w\.-]+@[\w\.-]+\.[a-zA-Z]{2,}$"
ValidEmail = veRegEx.Test(email)
End Function ''''''''''''''''''''''''''''''''''''


That just makes sure that some word character, period, or hyphen is followed by an @ sign, which is followed by another word character [A-Za-z0-9_], period or hyphen, and that is followed by a dot and at least two alpha characters... and if anything, it helps prevent users from making syntax mistakes in their entry.

No matter HOW complicated you get, people can still fool your email validation, even if you use some method to send a request to the email server to see if the email address entered exists... so I just validate the basic syntax. After all, they could enter abuse@microsoft.com and it will validate as REAL email address no matter what.

:)

If you really want to validate an email address, the best thing to do is set a bit value to 0 in your database until someone has responded from the email address in question. That way you know it's real and you don't have to implement any complicated code - but I still think you should use at least a basic syntax checking regex as above. :D

Mhtml
10-10-2002, 05:56 AM
I am doing exactly that.
Thanks Whammy!:)

Mhtml
10-10-2002, 06:08 AM
I'm not sure what to do with that script, what values will be returned?


Don't worry, I figured it. TRUE/FALSE This my first involvement with a function.

whammy
10-10-2002, 06:15 AM
Yup, exactly... that function returns a boolean value (True or False). Like:

If ValidEmail(email) = True Then
Response.Write("Yay!")
Else
Response.Write("Yuck!")
End if

:)

Mhtml
10-10-2002, 06:26 AM
lol, I do have one problem though. One of my current email addresses is @ix.net.au so it doesn't work, and I'm guessing I'm not the only person in the world to have something like that.

How would you go about changing it so that it would accept that?
I've tried:

call ValidEmail(request.Form("emailaddy"))
Function ValidEmail(email) ''''''''''''''''''''''
Dim veRegEx
Set veRegEx = New RegExp
veRegEx.Pattern = "^[\w\.-]+@[\w\.-]+\.[a-zA-Z]{2,}$+\.+\."
ValidEmail = veRegEx.Test(email)
response.write(veRegEx.Test(email))
End Function ''''''''''''''''''''''''''''''''''''

Mhtml
10-10-2002, 06:41 AM
It works now, not sure why but I've got all day to figure it out!

call ValidEmail(request.Form("emailaddy"))
Function ValidEmail(email) ''''''''''''''''''''''
Dim veRegEx
Set veRegEx = New RegExp
veRegEx.Pattern = "^[\w\.-]+@[\w\.-]+\.[a-zA-Z]{2,}"
ValidEmail = veRegEx.Test(email)
response.write(veRegEx.Test(email))
End Function ''''''''''

whammy
10-10-2002, 04:54 PM
Uhh... I just tested

asdf@ix.net.au

and it matches the original regular expression with no problems. Don't tell me there's nothing before the @ sign, because as far as I know, that's not a valid email address...

So I'm guessing the error is not with the function (since I've been using it for awhile), but something else you're doing... besides, you don't need to Call the function, just do exactly as I showed to return a boolean value.

Also, Response.Write in a function after you have exited the function won't work.

(Not to mention you're messing up my pretty formatting! :mad: ;))

:confused:

Mhtml
10-11-2002, 04:42 AM
I have it working fine now, I read a tutorial on functions and about using regex.

There is something before the @ symbol it's just I didn't want to put it on here because it is my non-signup email so it is junk mail free.

Also I have fixed up your pretty formatting!:D

whammy
10-11-2002, 06:17 AM
Yay! :D

Mhtml
10-11-2002, 09:01 AM
LOL:D