Hello, I have the following code: <cfinput type="text" name="EMAIL" message="Please enter your email address!" required="true" value="" size="50" maxlength="200"/></td> Code (markup): How do I ensure that the user inputs a valid email address? I'm working on using cfmail to send a confirmation email to the user to activate his/her account with my webpage (with the help from unitedlocalbands) but what are some other options? (requiring an "@" sign in the field?) Thanks.
Something like this on your action page will work <CFIF IsDefined("Form.email")> <CFSET email = "#Form.email#"> <CFELSE> <CFSET email = ""> </CFIF> <CFSET Valid = true> <CFSET Error = ""> <cfif len(#form.email#) LTE 0> <CFSET Error = Error & "Email Address is empty.<BR>"> <CFSET Valid = False> <cfelse> <cfif REFindNocase("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$", email)> <cfelse> <CFSET Error = Error & " You entered an invalid email address.<BR>"> <CFSET Valid = False> </cfif> </cfif> <CFIF not Valid> <STRONG>Sorry, An error occurred</STRONG> <HR> <CFOUTPUT><font size="+1" color="660000">#Error#</font></CFOUTPUT> <EM>Please correct the error/errors</EM><br></font> <A HREF="javascript:history.go(-1)"><br>Click Here To Return</A> <cfabort> <CFELSE> The rest of your action page will go here </cfif> Code (markup):
If you are using html forms you could also use cfinput's validate attribute. That would perform client side validation with javascript. <cfinput type="text" name="email" validate="email" ...> http://livedocs.adobe.com/coldfusion/7/htmldocs/00000279.htm Then as unitedlocalbands says back it up with server side validation. For MX7+ you can either a) Use the IsValid("email", form.email) function http://livedocs.adobe.com/coldfusion/7/htmldocs/00000534.htm or b) Use the IsEmail(..) UDF from cflib http://www.cflib.org/udf.cfm?ID=216
Back to the registration page... I run a check on the action page to make sure the username isn't already taken. It works fine. Then I tried doing the same thing with the email address. But, it doesn't work. Regardless if the email address is already taken, it allows the new user to register with it! I wonder if it's a problem with the @ symbol. Here is my code for the action page: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <cfif isdefined("form.upload_now")> <cfset maxFileSize = (150 * 1024)> <cfset serverFileName = ""> <cfset clientFileName = ""> <cfset wasFileDeleted = false> <cfif len(trim(form.ul_path))> <cffile action="upload" filefield="ul_path" destination="#ExpandPath("/user_uploads")#" accept="image/jpeg, image/gif, image/jpg, image/svg, image/pjpeg, image/pjpg, image/png, image/x-png" nameconflict="makeunique"> <cfset serverFileName = CFFILE.serverFile> <cfset clientFileName = CFFILE.clientFile> <cfif CFFILE.fileSize GT maxFileSize> <cffile action="delete" file="#CFFILE.serverDirectory#\#CFFILE.serverFile#"> <cfset wasFileDeleted = true> </cfif> </cfif> </cfif> <CFSET Valid = true> <CFSET Error = ""> <cfoutput> <cfif isdefined("form.USER_NAME_NAME")> <cfquery datasource="mydatabase" name="check"> SELECT USER_NAME_NAME FROM TBLUSER WHERE USER_NAME_NAME = '#form.USER_NAME_NAME#' </cfquery> </cfif> </cfoutput> <cfif check.recordcount eq 1> <CFSET Valid = False> <cfelse> </cfif> <CFIF not Valid> This username is taken. <A HREF="javascript:history.go(-1)"><br>Click Here To Return</A> <cfabort> <CFELSE> <CFSET Valid2 = true> <cfoutput> <cfif isdefined("form.EMAIL")> <cfset v = #form.EMAIL#> <cfquery datasource="mydatabase" name="check2"> SELECT * FROM TBLUSER WHERE EMAIL = '#v#' </cfquery> <cfset v ="#check2.EMAIL#"> </cfif> </cfoutput> <cfif check2.recordcount eq 1> <CFSET Valid2 = False> <cfelse> </cfif> <CFIF not Valid2> This email address is already registered! Please click <a href="forgotten_password.cfm">here</a> to retrieve your password. <A HREF="javascript:history.go(-1)"><br>Click Here To Return</A> <cfabort> <CFELSE> <cfif wasFileDeleted> <p>Sorry your file is too big! Hit "back" on your browser, and please try again.</p> <cfelse> <CFQUERY DATASOURCE="mydatabase" NAME="TBLUSER"> INSERT INTO TBLUSER(USER_NAME_NAME, USER_PASS_PASS, SERVER_FILE_NAME, CLIENT_FILE_NAME, EMAIL, CONFIRMED, USER_FIRST_NAME, USER_LAST_NAME, USER_GENDER, USER_FAV_RECIPE) VALUES ( <cfqueryparam value="#form.USER_NAME_NAME#" cfsqltype="cf_sql_varchar"> , <cfqueryparam value="#form.USER_PASS_PASS#" cfsqltype="cf_sql_varchar"> , <cfqueryparam value="#serverFileName#" cfsqltype="cf_sql_varchar"> , <cfqueryparam value="#clientFileName#" cfsqltype="cf_sql_varchar"> , <cfqueryparam value="#form.EMAIL#" cfsqltype="cf_sql_varchar"> , <cfqueryparam value="#form.CONFIRMED#" cfsqltype="cf_sql_varchar"> , <cfqueryparam value="#form.USER_FIRST_NAME#" cfsqltype="cf_sql_varchar"> , <cfqueryparam value="#form.USER_LAST_NAME#" cfsqltype="cf_sql_varchar"> , <cfqueryparam value="#form.USER_GENDER#" cfsqltype="cf_sql_varchar"> , <cfqueryparam value="#form.USER_FAV_RECIPE#" cfsqltype="cf_sql_varchar"> ) </CFQUERY> Thank you for registering! Please check your email to complete your registration! Or, click <a href="index.cfm">here</a> to go back to the homepage and login! </p> </p> </p> </cfif> </cfif> </cfif> </body> </html> Code (markup):
What do you mean a problem with the @ symbol? This section of the code works fine for me, so it must be a problem with either your #FORM.Email# values, the values in your table, or the nesting of your cfif statements. The best way to find the problem is to output the value of #FORM.Email# and the check2 query to find out where its going wrong. ie Is it even running the check2 query, and if so what are the results. On a side note, use check2.recordcount GT 0 instead of EQ 1. If for some reason a duplicate email did make it into your db, EQ 1 would always allow subsequent duplicates because the record count (2,3,4..) would not be equal to one. Though technically the best way to avoid duplicates is to use NOT EXISTS in your INSERT query and check the ms sql record count. But that's another question altogether. <!--- create a test email value ---> <cfset FORM.Email = "abc@abc.com"> <CFSET Valid2 = true> <cfif isdefined("form.EMAIL")> <cfquery datasource="#mydatabase#" name="check2"> SELECT * FROM TBLUSER WHERE EMAIL = '#form.EMAIL#' </cfquery> <cfif check2.recordcount eq 1> <CFSET Valid2 = False> </cfif> </cfif> <CFIF not Valid2> This email address is already registered! Please click <a href="forgotten_password.cfm">here</a> to retrieve your password. <A HREF="javascript:history.go(-1)"><br>Click Here To Return</A> <cfabort> <CFELSE> Do something else ... </cfif> Code (markup):
Yeah, I was just dumbfounded. However, I am beginning to think you may be on the right track with the EQ 1 issue. I will make it GT 0... i'm confident it will work. Thanks!