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

Posted on 2011-10-25
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
    LVL 51

    Expert Comment

    CF function or javascript?
    LVL 51

    Expert Comment

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

    Author Comment

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

    Accepted Solution

    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

    Thanks, that worked!

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    iPhone looses session info 5 15
    Displaying Category Name with Count 16 45
    Build PDF in coldfusion 2 49
    <cffile cannot delete a file 4 32
    PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
    Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math ( While this method is not o…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now