Forum Moderators: open

Message Too Old, No Replies

Valid Email Validation

doesn't work

         

cmatcme

1:06 pm on Mar 20, 2005 (gmt 0)

10+ Year Member



I'm using this line of code

If Len(request("email"))=0 AND NOT IN request("email")="@" AND NOT IN request("email")="."

to check whether someone's email address has an @ or a . sign in it. If it doesn't it's invalid.

Problem is IN doesn't appear to be a valid function.

What's the way to go about this?

cmatcme

2:43 pm on Mar 20, 2005 (gmt 0)

10+ Year Member



The code has now become

 Dim str
email = request("email")

If Len("email")=0 OR NOT INSTR (1, email, "@") OR NOT INSTR (1, email, ".") Then
response.write "<u>Your email address is invalid</u>"
End If

But even when the email address is example@example.com the error message still springs up.

 Updated: 14:59 20/03/05 UT 

CaseyRyan

4:01 pm on Mar 20, 2005 (gmt 0)

10+ Year Member



I found a couple problems that I've fixed in the below code.
When asking for a request object, I always throw a trim and concatenate it with a string so that I'm insured getting at least an empty string back. That's not really an error on your part, just a suggestion.

You had doubleQuotes around email when checking the length.

Instr does not return a boolean. It returns a numeric value indicating the position of the character found in the string. It returns 0 if not found. I also removed some spacing you had in there.


Dim str
email = Trim(request("email") & "")
If (Len(email)=0) OR (InStr(1, email, "@")=0) OR (InStr(1, email, ".")=0) Then
response.write "<u>Your email address is invalid</u>"
End If

-=casey=-

cmatcme

7:45 pm on Mar 20, 2005 (gmt 0)

10+ Year Member



Thanks Casey!

lovethecoast

9:07 pm on Mar 20, 2005 (gmt 0)

10+ Year Member



Aye -- always trim strings.

Another thing in this case is to ensure a period appears *after* the @ sign. The way you have it now, "my.name@" would be considered valid, when obviously, it's not.

If you really want to get fancy, you'll ensure at least two characters before the period and at least 2, but no more than 3 after the period (unless it's one of the few specific domains that are greater than 4 chars -- like .aero).

cmatcme

5:16 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



Aye -- always trim strings.

What does trimming strings do?

lovethecoast

5:23 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



Trimming strings saves you, and those using your services, more headaches than you can even begin to imagine.

For instance, I was looking at a real estate website last night and typed in my town's name -- by accident, I had a space at the beginning of my town name. Of course, they searched their database and couldn't find anything listed in " anytown" -- I saw my mistake, but I'm a programmer and notice those things -- imagine how many don't?

For your email app, if you don't trim strings, the same thing could happen -- someone would have to log in using " hello@my.com" instead of "hello@my.com"

Anything you store in a database should be trimmed before going in -- and always check forms by trimming strings to ensure valid data was entered. For instance, for a "First Name" field, imagine someone simply entering a space? Their first name in your database is now " " -- had you trimmed it, you would have seen immediately that the field was blank and you can return an error.

mattglet

6:31 pm on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cmatcme-

The Trim() function removes all white space from the left and right ends of a string, leaving only characters.

i.e.

Trim(" testing ") would output "testing" (no spaces on the ends).

Trim("test ") would output "test" (again, no space)

This is a very good practice as outlined by the reasons above.

cmatcme

6:54 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



Thanks everyone! Gonna trim everything now.

________________________

RECIEVED ERRORS :: CAN'T WORK OUT REASON:

If trim(request ("password") = trim(request ("passwordconfirm") AND Len(request("visitor"))=>1 AND Len(request("visitor"))=<15 AND Len(request("password"))=<15 AND Len(request("visitor"))=>1 AND Len(request("full"))=<35 AND Not IsNumeric(request("full")) Then
----------^

It expected ')' where the arrow marks

Why?

______________________

And after thinking for 15 minutes I got it.

If trim(request ("password") = trim(request ("passwordconfirm")

should be changed to

If trim(request ("password")) = trim(request ("passwordconfirm"))

______________________

lovethecoast

1:05 am on Mar 22, 2005 (gmt 0)

10+ Year Member



Remember that a request with no designation will return form, then querystring, then cookie.

If this is on a form's response page, best to use request.form (if using a post method -- request.querystring for a get method)

taunon

3:41 am on Mar 23, 2005 (gmt 0)

10+ Year Member



Using Regular Expressions:

<%
Response.write isValidEmail("david@codetoad.com") & "<BR>"
Response.write isValidEmail("davidcodetoadcom")

Function isValidEmail(myEmail)
dim isValidE
dim regEx

isValidE = True
set regEx = New RegExp

regEx.IgnoreCase = False

regEx.Pattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
isValidE = regEx.Test(myEmail)

isValidEmail = isValidE
End Function%>

cmatcme

7:39 pm on Mar 23, 2005 (gmt 0)

10+ Year Member



Being new to this stuff, could you explain what that does. I presume it validates email

lovethecoast

4:08 am on Mar 24, 2005 (gmt 0)

10+ Year Member



The regular expressions *should* valididate email, but it has a few flaws in it.

It doesn't permit 1 letter email addresses (a@test.com).

It doesn't permit underscores at the start of an email address.

It doesn't allow double domain validation (abc@my.uk.co).

It doesn't email addresses with an ip address or port number.

Basically -- my recommendation is not to use it. There's just too many flaws. Email validation is a pain -- I've only seen a couple out there that address every variant. I ended up having to write my own.

mattglet

12:29 pm on Mar 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have one that fits, would you mind sharing?

cmatcme

5:01 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



I just wrote this one, would this work?

<%

If request("address") = "check" Then

Dim str
mail = trim(request("email"))

If Len("mail")>=8 Then
correct = "false"
ElseIf NOT instr(1, mail, "@") OR NOT instr(1, mail, ".") Then
correct = "false"
ElseIf Len(instr(1, mail, "."))>=4 Then
correct = "false"
ElseIf NOT Len(instr(1, mail, "@"))>=2 Then
correct = "false"
ElseIf instr(1, mail, "hotmail") OR instr(1, mail, "yahoo") OR instr(1, mail, "gmail") Then 'rules off free mail accounts
correct = "false"
ElseIf NOT instr(1, mail, "com") OR NOT instr(1, mail, "co.uk") OR NOT instr(1, mail, "net") OR NOT instr(1, mail, "org") Then ' Rules off acounts with funny extensions
correct = "false"
ElseIf instr(1, mail, "!", "£", "$", "%", "^", "&", "*", "+", "=", "\", "¦", "<", ">", "~", "#", "]", "[", "{", "}") Then
correct = "false"
End If

If correct = "false" Then
response.write "Your address is invalid or not permitted </body> </html>"
response.end
End If

'Begin your send mail operation

End If

%>

If it does, feel free to use!

I'm sure there are errors - it doesn't detect @s or .s :: I just checked

mrMister

6:34 pm on Mar 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just wrote this one, would this work?

LOL, yeah, if you want your server to spend a million times longer processing a request than it needs to!

I don't think I've ever seen so many elseif statements! I think you've got more in that one function than I have on my whole server!

Regular expressions are the way to go. Much faster and they're better code all round, easier to maintain, etc.

If you're short of a good one, then type regex library or regular expression library in to your favourite search engine. there are a few good libraries out there that will offer email validation regexs for a variety of requirements.