Skip to main content
Inspiring
June 7, 2010
Question

<cfinput type="checkbox" - way to validate if checkbox IS ticked ?

  • June 7, 2010
  • 3 replies
  • 5710 views

Can coldfusion validate a form who's checkbox must be ticked ?

ie if the checkbox isn't ticked (terms and conditions) bring up a coldfusion dialogue box saying "checkbox must be ticked before proceeding"  ?

This topic has been closed for replies.

3 replies

miwerner
Participant
January 13, 2011

um... use required="yes" and a message="you gots ta click the box"

Participating Frequently
June 7, 2010

You can bring up a javaScript prompt using required="yes" like this:

<cfinput type="checkbox" name="myCheckbox" message="Please check the box." required="yes" id="myCheckbox" value="checkbox">

But you should also use some sort of server-side validation when the form is submitted for folks that have JavaScript disabled.

<cfif NOT isDefined("FORM.myCheckbox")>
ERROR RESPONSE HERE
</cfif>


Ken Ford

Inspiring
June 7, 2010

You can bring up a javaScript prompt using required="yes" like this:

<cfinput type="checkbox" name="myCheckbox" message="Please check the box." required="yes" id="myCheckbox" value="checkbox">

Are you sure about that?  Dun't work for me on CF 8.0.1.

--

Adam

Participating Frequently
June 7, 2010
ilssac
Inspiring
June 7, 2010

There is no such thing as a "ColdFusion Dialogue".

Dialouges are the relm of the client.  You would need client logic to invoke on.  JavaScript is that common scripting language of the client.

ColdFusion has some wizards and special tags that will create some canned JavaScript for you.  One of these is a required property on a <cfinput...> tag.  IIRC this might work with check box controls, but I am not sure.

Either way, you need to write code to handle the form field not being submitted.  Whether you write the JavaScript, ColdFusion provides canned JS or you apply some other JS library, if the user has turned off JavaScript that code is not going to run.  That means that if the control is not selected, the form field will not be provided by the browser to the server when the form is submitted.

You should provide server side logic that checks the existance of that field, and display appropiate resonses if it does not exist.

Inspiring
June 7, 2010

I was just using the CFFORM built in validation and wanted a quick way to check if the checkbox had been ticked. when the user hit submit - its for terms and conditions