Home > Mobile >  How do I compare and verify user input field to stored data BEFORE to sending form in ColdFusion
How do I compare and verify user input field to stored data BEFORE to sending form in ColdFusion

Time:02-12

I’m updating a site for my brother who teaches training courses. He has a registration form on the site that collects name, age, address, etc. That information is sent to him through cfmail with a copy sent to the registrant. The registrants then mails in a check via snail-mail to complete the registration. (My brother does NOT want to use an online payment method.)

Included in the form is the course name, location and fee. He asked if it was possible to implement some sort of “Promo Code” to offer discounts to select users. I’ve added PromoCode and PromoCode_Fee columns in SQL and am able to make it all work throughout the process.

My problem is on the user end. If the user mistypes the PromoCode in the form, the app will obviously not register the discount, send the registration emails out with the standard fee, and store the registration info in the DB. The only way for the user to fix the PromoCode would be to re-register, which would re-send the emails and add a new registration to the DB.

What I’d like to do is verify that the user entered a valid PromoCode in the input field PRIOR to submitting the form by comparing what they typed to the PromoCode stored in the DB. If the PromoCode doesn’t match, add “Promo Code is invalid” under the input field.

I do this as a hobby, am self-taught and am not sure if it’s even possible (or good idea.) I imagine it’s not possible to do with ColdFusion and would most likely need some sort of JS or jQuery - both of which I’m pretty illiterate in.

I’ve been searching for hours to see if anyone had any similar questions, but have come up short. Any help or pointing me in the right direction would be greatly appreciated.

CodePudding user response:

Usually, you'd need to post some code that you've tried and isn't working. But you've outlined what you want and are just not sure where to start.

You can test just the value of the discount code before allowing the whole form to be submitted. You don't say how you're doing client-side form validation. I'd suggest using jQuery Validate to handle that, it's very easy to implement.

Form With Errors

3. Create Remote URL

The live demo form uses a Test Remote URL

5. Fix Remote URL

With the component working, update the javascript code to point to the new cfc. Omit the username parameter because the plugin will pass it to the ajax call automatically.

username: {
   ...,
   remote: "path/YourComponentName.cfc?method=validateUserName"
}

6. Test Form (.. are you sensing a theme?)

Test the final form again with both a valid and invalid usernames to confirm it works as expected.

Next Steps ...

Continue to expand the working example and learn more about the plugin features by adding some customization. The next steps are left as an exercise for the reader ...

  • Replace hard coded test logic in the cfc with a database lookup
  • Replace default error message "Please fix this field" with a more user friendly message
  • Change the appearance of valid and invalid fields using CSS
  • Related