This is a Coldfusion version of the PHP validation library I developed way back when. You can find it on github here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!-----------------------------------------------------------------------------------------------
 Function:  validateFields
 Purpose:   This function checks required fields for ANY webform. It takes two parameters:
            pageAttributes - ALL form field info (attributes) received by post page
            fieldInfo      - an array. Each index is a list of the form:
               "requirement,fieldname[,fieldname2 [,fieldname3, date_flag]],error message".
                fieldname2 is optional, and is only used for comparing one field to another.

             alid "requirement" strings are:
                 "required"    - field must be filled in
                 "digits_only" - field must contain digits only
                 "length=X"    - field has to be X characters long
                 "length=X-Y"  - field has to be between X and Y (inclusive)
                             characters long
                 "valid_email" - field has to be valid email address
                 "valid_date"  - field has to be a valid date
                           fieldname:  MONTH                 fieldname2: DAY
       fieldname3: YEAR
                           date_flag:  "later_date" / "any_date"
                 "same_as"     - fieldname is the same as fieldname2 (intended
                           for password comparison).
                 "range=X-Y"   - checks that a number has a value between
                           X and Y (inclusive).

   E.g. an area home phone number form field "<input type='text' name='homePhone_area' />" would be
   validated as follows:

     <cfset variables.fieldInfo = ArrayNew()>
     <cfset variables.fieldInfo[1] = "required,homePhone_area,Please enter area home phone">
     <cfset variables.fieldInfo[2] = "digits_only,homePhone_area,Home phone area must only contain digits">
     <cfset variables.fieldInfo[3] = "length=3,homePhone_area,Home phone area must be 3 digits long">

     <cfinvoke  
      component="#request.componentPath#.Error"
      method="validateFields">
       <cfinvokeargument name="pageAttributes" value="#attributes#">
       <cfinvokeargument name="fieldInfo" value="#variables.fieldInfo#">
     </cfinvoke>

   Notes:
   - The array position in which the error messages appear for a single form field IS important. If
   homePhone_area failed the first check (index 1), the function will NOT check the two following
   requirements (indexes 2 and 3). That way, the user won't get three error messages for one incorrectly
   filled in field.
   - Duplicate error messages are not returned.


   If any of the the form fields contain an error, the function redirects the user back to the original form
   and makes two new variables available in sessions in WDDX format:

     session.wddx_pageAttributes
     session.wddx_errorMessages

   So, form page should contain this code, where-ever you want the list of error messages to
   appear:
   
     <!--- if required, display form validation errors --->
      <cfif IsDefined("session.wddx_errorMessages")>
        <cfwddx action="wddx2cfml" input="#session.wddx_errorMessages#" output="variables.errMessages">
        <cfinvoke
        component="#request.componentPath#.Error"
        method="displayErrors">
          <cfinvokeargument name="errorMessages" value="#variables.errMessages#">
        </cfinvoke>
      </cfif>

    NOTES FOR UPGRADES:
      - consider adding "zip_canada", "zip_US", "zip_northAmerica" options.
      - I deliberately return a struct of "fieldname" => "error message", not just error messages,
        so you could use the fieldname data to HIGHLIGHT those fields that contain errors.
      - "is_alpha" requirement
      - add valid_date routine, which checks for valid dates - plus additional later_date,
        earlier_date options to check date being inputted is earlier or later than current date.
  ---------------------------------------------------------------------------------------------->
  <cffunction access="public" name="validateFields" output="true" displayName="validateFields"
   hint="Validates fields for any web form; returns a list of error messages">
    <cfargument name="pageAttributes" type="any" required="yes" displayname="pageAttributes">
    <cfargument name="fieldInfo" type="array" required="yes" default="" displayname="reqFieldInfo">

<!--- this stores a LIST of errors found. (An array of lists of form: 'fieldName,error message' --->
    <cfset variables.errors = ArrayNew(1)>    <!--- stores all errors --->

    <cfloop from="1" to="#ArrayLen(arguments.fieldInfo)#" index="i">

      <!--- split the required field information into its component parts --->
      <cfset variables.requirement  = ListFirst(arguments.fieldInfo[i], ",")>
      <cfset arguments.fieldInfo[i] = ListDeleteAt(arguments.fieldInfo[i], 1, ",")>
      <cfset variables.fieldName    = ListFirst(arguments.fieldInfo[i], ",")>
      <cfset arguments.fieldInfo[i] = ListDeleteAt(arguments.fieldInfo[i], 1, ",")>

      <!--- if we're doing a "same_as" validation, there's a second fieldname listed --->
      <cfif variables.requirement EQ "same_as">    
        <cfset variables.fieldName2   = ListFirst(arguments.fieldInfo[i], ",")>
        <cfset arguments.fieldInfo[i] = ListDeleteAt(arguments.fieldInfo[i], 1, ",")>

      <!--- if we're doing a "valid_date" validation, extract extra fields --->
      <cfelseif variables.requirement EQ "valid_date">
        <cfset variables.fieldName2   = ListFirst(arguments.fieldInfo[i], ",")>
        <cfset arguments.fieldInfo[i] = ListDeleteAt(arguments.fieldInfo[i], 1, ",")>
        <cfset variables.fieldName3   = ListFirst(arguments.fieldInfo[i], ",")>
        <cfset arguments.fieldInfo[i] = ListDeleteAt(arguments.fieldInfo[i], 1, ",")>
        <cfset variables.date_flag    = ListFirst(arguments.fieldInfo[i], ",")>
        <cfset arguments.fieldInfo[i] = ListDeleteAt(arguments.fieldInfo[i], 1, ",")>      
      </cfif>

      <!--- at this point, the error messsage is all that's left --->
      <cfset variables.errorMessage    = arguments.fieldInfo[i]>

     
      <!--- IF this field doesn't have an error already associated with it, error-check it --->
      <cfset variables.errorFound = false>
      <cfloop from="1" to="#ArrayLen(variables.errors)#" index="i">
        <cfif ListContains(variables.errors[i], fieldName)>
          <cfset variables.errorFound = true>
        </cfif>
      </cfloop>

      <cfif NOT errorFound>
        <!--- if the requirement is LENGTH, rename requirement to "length" for switch statement --->
        <cfif Find("length=", variables.requirement)>
          <cfset variables.lengthRequirements = variables.requirement>
          <cfset variables.requirement = "length">
        </cfif>
       
        <!--- if the requirement is RANGE, rename requirement to "range" for switch statement --->
        <cfif Find("range=", variables.requirement)>
          <cfset variables.rangeRequirements = variables.requirement>
          <cfset variables.requirement = "range">
        </cfif>

       
        <!--- verify the field passes the required test --->
        <cfswitch expression="#variables.requirement#">

          <cfcase value="required">
            <cfif NOT StructKeyExists(arguments.pageAttributes, variables.fieldName) OR
             arguments.pageAttributes[variables.fieldName] EQ "">

              <!--- if this error message isn't already contained in "errors", add it --->
              <cfset variables.errorAlreadyExists = false>
              <cfloop from="1" to="#ArrayLen(variables.errors)#" index="i">
                <cfif ListContains(variables.errors[i], variables.errorMessage) NEQ 0>
                  <cfset variables.errorAlreadyExists = true>
                </cfif>
              </cfloop>
              <cfif NOT variables.errorAlreadyExists>
                <cfset ArrayAppend(variables.errors, variables.fieldName & "," & variables.errorMessage)>
              </cfif>

            </cfif>
          </cfcase>

          <cfcase value="digits_only">
            <cfif REFind("[^[:digit:]]", arguments.pageAttributes[variables.fieldName])>
             
              <!--- if this error message isn't already contained in "errors", add it --->
              <cfset variables.errorAlreadyExists = false>
              <cfloop from="1" to="#ArrayLen(variables.errors)#" index="i">
                <cfif ListContains(variables.errors[i], variables.errorMessage)>
                  <cfset variables.errorAlreadyExists = true>
                </cfif>
              </cfloop>
              <cfif NOT variables.errorAlreadyExists>
                <cfset ArrayAppend(variables.errors, variables.fieldName & "," & variables.errorMessage)>
              </cfif>

            </cfif>
          </cfcase>

          <cfcase value="length">
            <cfset variables.lengthRequirements = Replace(variables.lengthRequirements, "length=", "")>
            <cfset variables.lengths            = ListToArray(variables.lengthRequirements, "-")>
   
            <cfif ArrayLen(variables.lengths) IS 2>

              <!--- check field length is between specified length range --->
              <cfif (Len(arguments.pageAttributes[variables.fieldName]) LT variables.lengths[1]) OR
                 (Len(arguments.pageAttributes[variables.fieldName]) GT variables.lengths[2])>

                <!--- if this error message isn't already contained in "errors", add it --->
                <cfset variables.errorAlreadyExists = false>
                <cfloop from="1" to="#ArrayLen(variables.errors)#" index="i">
                  <cfif ListContains(variables.errors[i], variables.errorMessage)>
                    <cfset variables.errorAlreadyExists = true>
                  </cfif>
                </cfloop>
                <cfif NOT variables.errorAlreadyExists>
                  <cfset ArrayAppend(variables.errors, variables.fieldName & "," & variables.errorMessage)>
                </cfif>
 
              </cfif>
           
            <cfelse>
              <!--- otherwise, the user wants the field contents to be a specific length --->
              <cfif Len(arguments.pageAttributes[fieldName]) NEQ variables.lengths[1]>

                <!--- if this error message isn't already contained in "errors", add it --->
                <cfset variables.errorAlreadyExists = false>
                <cfloop from="1" to="#ArrayLen(variables.errors)#" index="i">
                  <cfif ListContains(variables.errors[i], variables.errorMessage) NEQ 0>
                    <cfset variables.errorAlreadyExists = true>
                  </cfif>
                </cfloop>
                <cfif NOT variables.errorAlreadyExists>
                  <cfset ArrayAppend(variables.errors, variables.fieldName & "," & variables.errorMessage)>
                </cfif>

              </cfif>            
            </cfif>
          </cfcase>
         
          <cfcase value="valid_email">
            <cfif NOT REFindNoCase("^['_a-z0-9-]+(\.['_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$", arguments.pageAttributes[fieldName])>

              <!--- if this error message isn't already contained in "errors", add it --->
              <cfset variables.errorAlreadyExists = false>
              <cfloop from="1" to="#ArrayLen(variables.errors)#" index="i">
                <cfif ListContains(variables.errors[i], variables.errorMessage) NEQ 0>
                  <cfset variables.errorAlreadyExists = true>
                </cfif>
              </cfloop>
              <cfif NOT variables.errorAlreadyExists>
                <cfset ArrayAppend(variables.errors, variables.fieldName & "," & variables.errorMessage)>
              </cfif>

            </cfif>
          </cfcase>

          <cfcase value="valid_date">
         
            <!--- included for future extensibility --->
            <cfif variables.date_flag EQ "later_date">
              <cfset variables.isLaterDate = true>
            <cfelse>
              <cfset variables.isLaterDate = false>            
            </cfif>

            <cfinvoke
            component="#request.componentPath#.Error"
            method="isValidDate"
            returnvariable="variables.isValid">
              <cfinvokeargument name="date_month" value="#arguments.pageAttributes[variables.fieldName]#"/>
              <cfinvokeargument name="date_day" value="#arguments.pageAttributes[variables.fieldName2]#"/>
              <cfinvokeargument name="date_year" value="#arguments.pageAttributes[variables.fieldName3]#"/>
              <cfinvokeargument name="isLaterDate" value="#variables.isLaterDate#"/>
            </cfinvoke>

            <!--- if this error message isn't already contained in "errors", add it --->
            <cfif NOT variables.isValid>
              <cfset variables.errorAlreadyExists = false>
              <cfloop from="1" to="#ArrayLen(variables.errors)#" index="i">
                <cfif ListContains(variables.errors[i], variables.errorMessage) NEQ 0>
                  <cfset variables.errorAlreadyExists = true>
                </cfif>
              </cfloop>
              <cfif NOT variables.errorAlreadyExists>
                <cfset ArrayAppend(variables.errors, variables.fieldName & "," & variables.errorMessage)>
              </cfif>
            </cfif>
          </cfcase>

          <cfcase value="same_as">
            <cfif arguments.pageAttributes[fieldName] NEQ arguments.pageAttributes[fieldName2]>

              <!--- if this error message isn't already contained in "errors", add it --->
              <cfset variables.errorAlreadyExists = false>
              <cfloop from="1" to="#ArrayLen(variables.errors)#" index="i">
                <cfif ListContains(variables.errors[i], variables.errorMessage) NEQ 0>
                  <cfset variables.errorAlreadyExists = true>
                </cfif>
              </cfloop>
              <cfif NOT variables.errorAlreadyExists>
                <cfset ArrayAppend(variables.errors, variables.fieldName & "," & variables.errorMessage)>
              </cfif>

            </cfif>
          </cfcase>

          <cfcase value="range">
            <cfset variables.rangeRequirements  = Replace(variables.rangeRequirements, "range=", "")>
            <cfset variables.range              = ListToArray(variables.rangeRequirements, "-")>    

            <!--- check field value is between specified number range --->  
            <cfif (arguments.pageAttributes[variables.fieldName] LT variables.range[1]) OR
               (arguments.pageAttributes[variables.fieldName] GT variables.range[2])>

              <!--- if this error message isn't already contained in "errors", add it --->
              <cfset variables.errorAlreadyExists = false>
              <cfloop from="1" to="#ArrayLen(variables.errors)#" index="i">
                <cfif ListContains(variables.errors[i], variables.errorMessage) NEQ 0>
                  <cfset variables.errorFound = true>
                </cfif>
              </cfloop>
              <cfif NOT variables.errorAlreadyExists>
                <cfset ArrayAppend(variables.errors, variables.fieldName & "," & variables.errorMessage)>
              </cfif>

            </cfif>
          </cfcase>

          <cfdefaultcase>
            Unknown requirement in Error.validateFields: <b>#variables.requirement#</b>
            <cfabort>
          </cfdefaultcase>
        </cfswitch>

      </cfif>
    </cfloop>


    <!--- if there are validation errors, return attribute contents and error messages to referer page --->
    <cfif NOT ArrayIsEmpty(variables.errors)>

      <!--- convert the error message array into a wddx string in order to send it to form page --->
      <cfwddx action="cfml2wddx" input="#variables.errors#" output="variables.wddx_errorMessages" usetimezoneinfo="no">
      <cfwddx action="cfml2wddx" input="#arguments.pageAttributes#" output="variables.wddx_pageAttributes" usetimezoneinfo="no">

      <!--- store this information in a (temporary) session, then redirect. Note:
           this session info is destroyed once the user fills in the form correctly. --->      
      <cflock scope="session" type="Exclusive" timeout="20" throwontimeout="yes">
        <cfset session.wddx_errorMessages  = variables.wddx_errorMessages>
        <cfset session.wddx_pageAttributes = variables.wddx_pageAttributes>
      </cflock>

      <cflocation url="#CGI.HTTP_REFERER#" addtoken="no">
    </cfif>
  </cffunction>