How To Check For Valid Email

Snitz™ Forums 2000
https://forum.snitz.com/forumTopic/Posts/67552?pagenum=1
05 November 2025, 02:40

Topic


leatherlips
How To Check For Valid Email
31 July 2008, 21:54


I have a page where the user enters an email address. What code would I use to make sure the email address is in a valid format? I don't need to verify it. I just want to make sure they enter something like abc@abc.com. If they don't then I want them to receive an error about the email not being valid and to go back and enter a valid one.<

 

Replies ...


phy1729
31 July 2008, 22:37


http://www.regular-expressions.info/email.html<
AnonJr
31 July 2008, 23:54


I seem to remember there was a ChkEMail function (or something similar) in inc_func_common.asp. It would be a good place to start...<
Andy Humm
26 August 2008, 10:41


AnonJr: I have had a look through inc_func_common and can not see an email validity check routine. Like you say, I am sure there is a routine, somewhere within the forum, to meet leatherlips requirement. Any further ideas please..<
ruirib
26 August 2008, 10:56


Posted sometime ago as a security fix replacement for an existing function in inc_func_member.asp:
Code:

function EmailField(fTestString) 
set regEx = New RegExp
regEx.Global = true
regEx.IgnoreCase = true
regEx.Pattern = "^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$"

retVal = regEx.Test(fTestString)

set regEx = nothing

if Not retVal Then
EmailField = 0
Else
EmailField = 1
end if
end function
<
Andy Humm
26 August 2008, 12:09


thank u ruirib<
Andy Humm
26 August 2008, 12:55


Is there an easy way to link this function Emailfield to my input field:

response.write " <input type=""text"" style=""background-color:#F5FBF1"" name=""frommail"" size=""50"">" & vbNewLine

I have added the function to my inc_func_common and can not see how to tie the functionality together with appropriate error message to be displayed - if a user inputs a wrong email string format


The code I have on the form preview is:
frommail = trim(chkString(request.form("frommail"),"SQLString")) if frommail ="" then strError = strError & "Please enter your email address!<br>" 'checking for blank email field
if (InStr(Request("tomail"), "@") = 0 or InStr(Request("frommail"), "@") = 0) then strError = strError & "Enter an e-mail address in the form username@domain.co.uk<br></font>"

But is a user puts an email in this format humbug@xmas the routine passes, (that is the code I have)
Thank you andy




<
HuwR
26 August 2008, 13:39


The function should replace the existing EmailField function in inc_func_member.asp so you will also need to include inc_func_member.asp

you would then use it like so

if EmailField(frommail) = 1 then
'we passed so do stuff :)
else
'we failed so error
end if<
Andy Humm
26 August 2008, 18:28


Thank u HuwR<
Andy Humm
26 August 2008, 19:24


I currently do not have access to my forum files, but following Huwrs input, would this work:

if EmailField(frommail) = 0 then strError = strError & "Enter an e-mail address in the form username@domain.co.uk<br></font>"
... .. end if (later)

I have gone for the =0 as all the other error messages have a link underneath to return to input form.<
HuwR
27 August 2008, 01:50


yes, that should be fine.<
Andy Humm
27 August 2008, 03:58


Huwr: Thank you, all works fine now.. next!<
Shaggy
29 August 2008, 06:43


Thanks, Rui; was looking for a RegEx address validator smile
<
ruirib
29 August 2008, 06:56


Originally posted by Shaggy
Thanks, Rui; was looking for a RegEx address validator smile
No problem smile. This is not a complete solution, even if it should be fine for "normal" use.<
Shaggy
29 August 2008, 06:59


Seems to be doing the trick for me so far smile What problems have you encountered with it?
<
HuwR
29 August 2008, 07:04


Originally posted by Shaggy
Thanks, Rui; was looking for a RegEx address validator smile

here is better one smile, the one posted above does not accept all the valid characters for an email address, the one below should.
\w+([!#$%*/?|^{}`~&'+-=_]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*<
ruirib
29 August 2008, 07:05


I haven't had problems. I got it from a book or online and I remember that it stated that a couple "obscure" cases weren't contemplated. I can't remember what cases were not contemplated, but they were not relevant enough to justify a much more complex regex.<
Shaggy
29 August 2008, 07:15


Thanks, Huw; have replaced the RegEx with your one. Looks like this was the source of your one, Rui - some interesting, over-the-top alternatives in that article.
<
ruirib
29 August 2008, 07:27


Yes, Shaggy, I guess that was the one.<
HuwR
29 August 2008, 07:46


actually mine needs a slight adjustment

\w*([!#$%*/?|\^{}`~&'+-=_])*\w*@\w+([-.]\w+)*\.\w+([-.]\w+)*

it was not allowing this address a!#$%&'*+-/=?@example.com which believe it or not is perfectly valid.
there are still a few cases that won't validate, but they would be very unusual.<
HuwR
29 August 2008, 07:54


sorry, another tweak, this should be much better

[A-Z0-9!#$%*/?|\^{}`~&'+-=_]*@\w+([-.]\w+)*\.\w+([-.]\w+)*<
Shaggy
29 August 2008, 09:16


Thanks, again, Huw; will add that to the script when I get back on the other machine.
<
© 2000-2021 Snitz™ Communications