Skip to main content
Inspiring
July 17, 2007
Question

Stupid Noob Question About Form Validation

  • July 17, 2007
  • 6 replies
  • 771 views
I can't get this code to work. Can some kind-hearted soul look at it and tell me what stupid thing I'm doing wrong? It processes the output of a contact or email form. The field "status" is indeed defined on the form as a hidden field. When I enter appropriate text in the fields of the form, it takes me to "error.cfm" every time. Here's what I'm entering in the form for testing purposes:

fromemail = crazy@windmills.com
yourname = Jack
phone = 407-755-0757
message = Message

Here's the code that processes the form output:

<cfset emailIndex = FindOneOf("@aeiou", form.fromemail)>
<cfset nameIndex = FindOneOf("aeiou", form.yourname)>
<cfset phoneIndex = FindOneOf("123456789",form.phone)>
<cfset messageIndex = FindOneOf("aeiou",form.message)>

<!--- Check to see if the form was submitted. Also check for valid data in each form field. --->
<cfif isDefined("form.status") AND form.status eq "sendemail" AND emailIndex GT 1 AND nameIndex GT 1 AND phoneIndex GT 1 AND messageIndex GT 1>
<!--- If the form was submitted, send the email! --->
<!-- NOTE: Hosting company email security requires that the username, password, and server name be included in the ColdFusion code. -->
<cfmail to="someone@domain.com"
from="someone@domain.com"
username="someone@domain.com"
password="correct_password_verified"
server="correct_server_verified"
replyto="#form.fromemail#"
subject="Email From website"
failto="validEmail@domain.com">
This message is from: #Form.yourname#, #form.fromemail#, #form.phone#

#FORM.message#

</cfmail>
<cflocation url="success.cfm">
<cfelse>
<cflocation url="error.cfm">
</cfif>

The form is located at http://www.GwenHarrison.com/contact/contactme.cfm.

    6 replies

    GwenHAuthor
    Inspiring
    July 20, 2007
    Thanks to everyone who responded ... with your help I was able to get it working. I'm going to look into replacing that method with regex as soon as I finish another project I'm working on.

    Gwen H
    GwenHAuthor
    Inspiring
    July 20, 2007
    Thanks to everyone who responded ... with your help I was able to get it working. I'm going to look into replacing that method with regex as soon as I finish another project I'm working on.
    Inspiring
    July 19, 2007
    The "error.cfm" only appears when the initial <cfif> statements comes back as false. Meaning if it fails ANY of the criteria it will go to "error.cfm".

    1. What is "FORM.status" -- is that the submit button?
    a. If yes to above, then is "sendemail" the name of the submit button?
    2. Instead of doing "emailIndex GT 1 AND nameIndex GT 1 AND phoneIndex GT 1 AND messageIndex GT 1" do this:
    ---------------------------------------------------------
    <cfif isDefined("FORM.status") and FORM.status eq "sendemail" and isdefined("FORM.fromemail") and FORM.fromemail NEQ "" and isdefined("FORM.yourname") and FORM.yourname NEQ "" and isdefined("FORM.phone") and FORM.phone NEQ "" and isdefined("FORM.message") and FORM.message NEQ "">
    HI
    <cfelse>
    <cfdump var="#form#">
    </cfif>
    ---------------------------------------------------------

    Report back what you see.
    Inspiring
    July 18, 2007
    You're doing better than I would ;)

    I don't think the phone index meets the criteria. The index value is 0.

    Btw, is FindOneOf() really the best tool for the job? Regex's take some getting used to but there are some pretty standard ones out there like: check for valid email address, phone number, etc. Another option is to use cfform and cfinput fields. They also have some standard checks for email addresses, etc.

    Inspiring
    July 18, 2007
    Nope. That doesn't look like the correct output for the code I posted :) Look at the code again. Your CFIF statement uses tests a bunch of "Index" fields. That's what you should be outputting.

    GwenHAuthor
    Inspiring
    July 18, 2007
    Now I've comprehended what you wanted. I'm sitting here trying to work on three different websites and keep up with this thread - which is a real stretch for me. Anyways, I did as you suggested, and according to the output all the field data meets the criteria I've established for each field. You can go to the link I provided in my last post, enter numbers in the phone field, leave the other fields as they are, and submit the form, to see the results.
    Inspiring
    July 18, 2007
    quote:

    Originally posted by: GwenH
    Now I've comprehended what you wanted. I'm sitting here trying to work on three different websites and keep up with this thread - which is a real stretch for me. Anyways, I did as you suggested, and according to the output all the field data meets the criteria I've established for each field. You can go to the link I provided in my last post, enter numbers in the phone field, leave the other fields as they are, and submit the form, to see the results.

    Working on three sites at the same time is a bad idea.

    When if/else logic isn't behaving the way I think it should, I usually do something like this:

    <cfif SomeVariable is SomeExpectedValue>
    code
    <cfelse>
    <cfdump var="#SomeVariable#">
    </cfif>
    Inspiring
    July 17, 2007
    start with <cfdump var="#form#">
    GwenHAuthor
    Inspiring
    July 18, 2007
    Okey dokey, I created a plain page whose only content is <p><cfdump expand="yes" var="#form"></p>. You can see the output by going to www.GwenHarrison.com/contact/contactme.cfm, entering data in the form, and submitting it.

    I'm sure this is supposed to tell me something useful, but as a stupid noob I've no idea what.

    Thanks!
    Gwen Harrison