[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Function for if one field is entered, then they are all required

Posted on 2011-10-25
Medium Priority
Last Modified: 2012-05-12
I wanted to see if there was an easy way to create a function that takes in five fields (name, address, city, state, zip). If any one of the fields is entered, then all the fields are required. However, if they are all blank, then none are required.  Right now I have a series of if-else statements which is quite cumbersome.
Question by:manicdiggity
  • 3
  • 2
LVL 52

Expert Comment

ID: 37027346
CF function or javascript?
LVL 52

Expert Comment

ID: 37027373
... and what should this function do if one of the required fields is missing?

Author Comment

ID: 37027503
CF function. If one of the required fields is missing, it can just return text that all fields must be entered. Thanks!
LVL 52

Accepted Solution

_agx_ earned 1000 total points
ID: 37027653
I'd recommend returning only true/false so it's a little more flexible.

<cfset success = isValidAddress(name="a",address="b",city="c",state="d",zip="e")>
<cfif NOT success>
      display your text ie all fields must be entered.

<cffunction name="isValidAddress" returntype="boolean">
	<cfargument name="name" type="string" required="false">
	<cfargument name="address" type="string" required="false">
	<cfargument name="city" type="string" required="false">
	<cfargument name="state" type="string" required="false">
	<cfargument name="zip" type="string" required="false">
	<cfset var key = "">
	<cfset var nonEmptyValues = 0>
	<cfloop collection="#arguments#" item="key">
		<cfif structKeyExists(arguments, key) and len(trim(arguments[key]))>
			<cfset nonEmptyValues = nonEmptyValues + 1>

	<cfif nonEmptyValues and nonEmptyValues lt structCount(arguments)>
		<cfreturn false >	
		<cfreturn true>

Open in new window


Author Closing Comment

ID: 37027973
Thanks, that worked!

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question