Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 656
  • Last Modified:

Dropdown box validation in Coldfusion

This is probably an easy question for you guys but I'm drawing a blank. I have a form that users submit to me. A couple of the fields are dropdown boxes. eg. The title of the field is Select Your Sport and in the dropdown box is a list of sports. Now i have a javascript that checks to see if different fields are filled in within my form but the dropdowns are where i have my problem. Currently the highlighted selection is "Select Your Sport". If a user fills in all required information but forgets to select their sport, i get an email with the info they submitted but in the sport field it says their sport is "Select Your Sport" and not soccer, football, baseball etc.. How can I make the Select your Sport option visible but validate it somehow so that if no sport is selected they will get a message saying "You must select your sport". Does this make sense? I am using <cfform> and have posted my <cfselect> code below.

<cfselect name="sport" message="You must choose a sport to process an update!" required="yes" multiple="no">
                <option selected>Select Your Sport</option>
      <option value="Football">Football</option>
      <option value="Boys Soccer">Boys Soccer</option>
      <option value="Girls Soccer">Girls Soccer</option>
      <option value="Field Hockey">Field Hockey</option>
      <option value="Girls Volleyball">Girls Volleyball</option>
      <option value="Girls Tennis">Girls Tennis</option>
      <option value="Boys Cross Country">Boys Cross Country</option>
      <option value="Girls Cross Country">Girls Cross Country</option>
      <option value="Boys Golf">Boys Golf</option>
</cfselect>
0
IShiva
Asked:
IShiva
2 Solutions
 
pinaldaveCommented:
you can write this code on the submitted page
<cfif form.sport eq 'Select Your Sport'>
You must select your sport
</cfif>

also you need to change your first option like this...   <option value="Select Your Sport"  selected>Select Your Sport</option>

Regards,
---Pinal
0
 
mrichmonCommented:
You could use pinaldave's solution, but that validation is server side and you probably want it to happen client side - especially since you have other clientside validation.

I would add a javascript function

function CheckSelect()
{
if(f (the_form.sport.selectedIndex == 0)
{

// If the first choice is selected display an alert box
// stating the first choice is not a valid selection
alert("Please select a sport.");

// Focus on the dropdown menu after OK is clicked from the alert box
the_form.sport.focus();
return (false);
}

Then in the form

 onsubmit="return CheckSelect()"
0
 
IShivaAuthor Commented:
Thanks for the options guys!

IShiva
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now