55502f40dc8b7c769880b10874abc9d0

After a post submission I would like to validate a single text field called "tags." Testing for its presence is required since some objects do not require the field.

I have two criteria: 1) A maximum of 5 tags, and 2) a maximum length of 25 characters per tag. Since the tags are all inserted into the same field, I am converting them into an array, testing against the array length, then testing against the length of each item in the array.

Someone mentioned I do not need to use the array. I think this is true, but I also read that working with arrays is far quicker than working with lists in coldfusion. Is this true?

<cffunction name="validateTags" access="private" output="false" hint="This function validates the length of each tag, and the number total tags.">

		<cfif StructKeyExists(this, "tags")>

			<cfset tagArray = ListToArray(this.tags)>

			<cfif ArrayLen(tagArray) GT 5>

            	<cfset this.addError(property="tags", message="You can only use up to 5 tags.")>

			<cfelse>

                <cfloop index="i" from="1" to="#ArrayLen(tagArray)#">
    
                    <cfif Len(tagArray[i]) GT 25>
                        <cfset this.addError(property="tags", message="A tag can be a maximum of 25 characters.")>
                    </cfif>
    
                </cfloop>

			</cfif>

        </cfif>

    </cffunction>

Refactorings

No refactoring yet !

Your refactoring





Format Copy from initial code

or Cancel