How To Check For Valid Email - Posted (1902 Views)
Senior Member
leatherlips
Posts: 1838
1838
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.<
 Sort direction, for dates DESC means newest first  
 Page size 
Posted
Average Member
phy1729
Posts: 589
589
http://www.regular-expressions.info/email.html<
Posted
Forum Moderator
AnonJr
Posts: 5768
5768
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...<
Posted
Average Member
Andy Humm
Posts: 908
908
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..<
Posted
Snitz Forums Admin
ruirib
Posts: 26364
26364
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
<
Posted
Average Member
Andy Humm
Posts: 908
908
thank u ruirib<
Posted
Average Member
Andy Humm
Posts: 908
908
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




<
Posted
Forum Admin
HuwR
Posts: 20611
20611
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<
Posted
Average Member
Andy Humm
Posts: 908
908
Thank u HuwR<
Posted
Average Member
Andy Humm
Posts: 908
908
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.<
Posted
Forum Admin
HuwR
Posts: 20611
20611
yes, that should be fine.<
You Must enter a message