Forum Moderators: open
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?
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
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=-
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).
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.
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.
________________________
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"))
______________________
<%
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%>
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.
<%
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
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.