Subversion Repositories DevTools

Rev

Rev 5596 | Rev 5838 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
119 ghuddy 1
<%
2
'=============================================================
3
'//
129 ghuddy 4
'//                  Validation Control
119 ghuddy 5
'//
6
'=============================================================
7
%>
8
<%
9
Class ValidationControl
129 ghuddy 10
 
11
   Private maRules()
12
   Private mobjFieldMap
13
   Private mobjErrorMsg
14
   Private mobjAltVal
15
 
16
   Private InxFieldName
17
   Private InxIsRequired
18
   Private InxIsNumeric
19
   Private InxMinNumericValue
20
   Private InxMaxNumericValue
21
   Private InxIsDate
22
   Private InxStartDate
23
   Private InxEndDate
24
   Private InxMinStringLength
25
   Private InxMaxStringLength
26
   Private InxRegExp
27
   Private InxRegExpDescription
28
 
29
   Private mNumOfRules
30
   Private mLastRuleInx
31
 
32
   Private bIsFormValid
33
   Private bIsValidated
34
   Private bIsPostBack
35
   Private bHiddenTagPlanted
36
 
37
   Private sPostBackTagName
38
   Private sBULET
39
 
40
 
41
   Public Property Get IsPostBack ()
42
      IsPostBack = bIsPostBack
43
   End Property
44
 
45
   Public Property Get IsValidOnPostBack
46
      If bIsValidated Then
47
         IsValidOnPostBack = bIsFormValid
48
      Else
49
         IsValidOnPostBack = ValidateForm()
50
      End If
51
 
52
   End Property
53
 
54
   '-----------------------------------------------------------------------------------------------------------------
55
   Public Function GetValue ( sFieldName, altValue )
56
      If NOT bIsPostBack Then
57
         GetValue = altValue
58
      Else
59
         GetValue = Request(sFieldName)
60
      End If
61
 
62
      mobjAltVal.Item ( sFieldName ) = GetValue
63
   End Function
64
   '-----------------------------------------------------------------------------------------------------------------
65
   Public Sub SetValue ( sFieldName, altValue )
66
      If NOT bIsPostBack Then
67
         mobjAltVal.Item ( sFieldName ) = altValue
68
      Else
69
         mobjAltVal.Item ( sFieldName ) = Request(sFieldName)
70
      End If
71
 
72
   End Sub
73
   '-----------------------------------------------------------------------------------------------------------------
74
   Public Sub SetValueForced ( sFieldName, value )
75
      mobjAltVal.Item ( sFieldName ) = value
76
 
77
   End Sub
78
   '-----------------------------------------------------------------------------------------------------------------
79
   Private Function RequestValue( sFieldName )
80
      If NOT bIsPostBack Then
81
         RequestValue = mobjAltVal.Item ( sFieldName )
82
      Else
83
         If Request(sFieldName) <> "" Then
84
            RequestValue = Request(sFieldName)
85
         Else
86
            RequestValue = mobjAltVal.Item ( sFieldName )
87
         End If
88
      End If
89
 
90
   End Function
91
   '-----------------------------------------------------------------------------------------------------------------
92
   Public Function IsTicked( sFieldName, nParId, altValue )
93
      Dim sParList
94
 
95
      nParId     = ","&  Replace(nParId, " ", "") &","
96
      sParList = ","&  Replace( Request(sFieldName), " ", "") &","
97
 
98
      If NOT bIsPostBack Then
99
         IsTicked = (NOT IsNull(altValue))  OR  (altValue <> "")
100
 
101
      Else
102
         If InStr( sParList, nParId ) > 0 Then
103
            IsTicked = TRUE
104
         Else
105
            IsTicked = FALSE
106
         End If
107
 
108
      End If
109
 
110
   End Function
5596 dpurdie 111
 
129 ghuddy 112
   '-----------------------------------------------------------------------------------------------------------------
5596 dpurdie 113
   Public Function AddPostBack ()
129 ghuddy 114
      '--- Plant Hidden Tag
115
      If NOT bHiddenTagPlanted Then
116
         ' This tag is used by this class to know if the form is posted back
117
         Response.write "<input type='hidden' name='"& sPostBackTagName &"' value='true'>"
118
         bHiddenTagPlanted = TRUE
119
      End If
5596 dpurdie 120
   End Function
129 ghuddy 121
 
5596 dpurdie 122
   '-----------------------------------------------------------------------------------------------------------------
123
   Public Function Validate ( sFieldName )
124
 
125
      '--- Plant Hidden Tag
126
      AddPostBack()
127
 
129 ghuddy 128
      Call ValidateField ( RequestValue(sFieldName), mobjFieldMap.Item ( Cstr( sFieldName  ) ) )
129
 
130
      Validate = GetErrorMsg ( sFieldName )
131
 
132
   End Function
133
   '-----------------------------------------------------------------------------------------------------------------
134
   Private Function ValidateForm()
135
      Dim i, FieldValue, nLastRowInx
136
      mobjErrorMsg.RemoveAll   ' Clean Error Messages
137
 
138
      nLastRowInx = LastRowInx ()
139
 
140
      For i = 0 To nLastRowInx
141
         FieldValue = RequestValue( maRules( InxFieldName, i ) )
142
 
143
         Call ValidateField ( FieldValue, i )
144
 
145
      Next
146
 
147
      ' --- Finally, set the Form state of validity
148
      If mobjErrorMsg.Count > 0 Then
149
         bIsFormValid = FALSE
150
      Else
151
         bIsFormValid = TRUE
152
      End If
153
 
154
 
155
      bIsValidated = TRUE
156
      ValidateForm = bIsFormValid
157
 
158
   End Function
159
   '-----------------------------------------------------------------------------------------------------------------
160
   Private Sub ValidateField ( FieldValue, i )
161
 
162
      If (i = "") OR (IsNull(i)) Then
163
         Err.Raise 8, "Cannot Find Field Name.", "Make sure you have correct filed names listed for validation."
164
         Exit Sub
165
      End If
166
 
167
      If mobjErrorMsg.Exists ( CStr( maRules( InxFieldName, i ) ) ) Then mobjErrorMsg.Remove ( CStr( maRules( InxFieldName, i ) ) )   ' Clean this field Error Messages
168
 
169
      ' RULE is_Required
170
      If NOT ValidForIsRequired ( FieldValue, i ) Then
171
         Call AddErrorMessage ( i, "Required." )
172
 
173
      Else
174
 
175
         If FieldValue <> "" Then
176
 
177
            ' --- RULE is_Number ---
178
            If NOT ValidForIsNumeric ( FieldValue, i ) Then
179
               Call AddErrorMessage ( i, "Must be a number." )
180
            Else
181
 
182
               If maRules( InxIsNumeric, i ) = enumDB_YES Then  ' Continue if field is a Number
183
                  ' --- RULE min_Numeric_value ---
184
                  If NOT ValidForMinNumericValue ( FieldValue, i ) Then
185
                     Call AddErrorMessage ( i, "Must be minimum "& maRules( InxMinNumericValue, i ) &"." )
186
                  End If
187
 
188
                  ' --- RULE max_Numeric_value ---
189
                  If NOT ValidForMaxNumericValue ( FieldValue, i ) Then
190
                     Call AddErrorMessage ( i, "Must be maximum "& maRules( InxMaxNumericValue, i ) &"." )
191
                  End If
192
               End If
193
 
194
            End If
195
 
196
 
197
            ' --- RULE is_Date ---
198
            If NOT ValidForIsDate ( FieldValue, i ) Then
199
               Call AddErrorMessage ( i, "Must be a date." )
200
            Else
201
 
202
               If maRules( InxIsDate, i ) = enumDB_YES Then  ' Continue if field is a Date
203
                  ' --- RULE start_Date ---
204
                  If NOT ValidForStartDate ( FieldValue, i ) Then
205
                     Call AddErrorMessage ( i, "Cannot be before "& maRules( InxStartDate, i ) &"." )
206
                  End If
207
 
208
                  ' --- RULE end_Date ---
209
                  If NOT ValidForEndDate ( FieldValue, i ) Then
210
                     Call AddErrorMessage ( i, "Cannot be after "& maRules( InxStartDate, i ) &"." )
211
                  End If
212
 
213
               End If
214
 
215
            End If
216
 
217
 
218
            ' --- RULE min_String_Length ---
219
            If NOT ValidForMinStringLength ( FieldValue, i ) Then
220
               Call AddErrorMessage ( i, "Must be at least "& maRules( InxMinStringLength, i ) &" character(s) long." )
221
            End If
222
 
223
            ' --- RULE min_String_Length ---
224
            If NOT ValidForMaxStringLength ( FieldValue, i ) Then
225
               Call AddErrorMessage ( i, "Can be maximum "& maRules( InxMaxStringLength, i ) &" character(s) long." )
226
            End If
227
 
228
 
229
            ' --- RULE Regular Expression ---
230
            If NOT ValidForRegExp ( FieldValue, i ) Then
231
               Call AddErrorMessage ( i, maRules( InxRegExpDescription, i ) )
232
            End If
233
 
234
 
235
 
236
         End If
237
 
238
 
239
      End If
240
   End Sub
241
   '-----------------------------------------------------------------------------------------------------------------
242
   Private Function ValidForIsRequired ( fieldValue, rowId )
243
      ValidForIsRequired = FALSE
244
      If maRules( InxIsRequired, rowId ) = enumDB_YES Then
245
         '/* Check if empty */
246
         If fieldValue <> "" Then
247
 
248
            '/* Check for spaces */
249
            If Len( Replace(fieldValue, " ", "") ) > 0 Then   ValidForIsRequired = TRUE
250
 
251
         End If
252
 
253
      Else
254
         ValidForIsRequired = TRUE
255
 
256
      End If
257
   End Function
258
   '-----------------------------------------------------------------------------------------------------------------
259
   Private Function ValidForRegExp ( fieldValue, rowId )
260
      Dim objRegEx
261
 
262
      ValidForRegExp = FALSE
263
      If IsNull( maRules( InxRegExp, rowId ) ) OR (maRules( InxRegExp, rowId ) = "") Then
264
         ValidForRegExp = TRUE
265
      Else
266
         Set objRegEx = New RegExp
267
 
268
         objRegEx.Global = False      ' Only find first match. This is enough to fail the validation
269
         objRegEx.IgnoreCase = False   ' Follow match pattern exactly.
270
          objRegEx.Pattern = maRules( InxRegExp, rowId )       ' Set the pattern to match
271
 
272
         ' Now test the pattern match.
273
         If NOT objRegEx.Test( fieldValue ) Then
274
            ValidForRegExp = TRUE
275
         End If
276
 
277
         Set objRegEx = Nothing
278
      End If
279
   End Function
280
   '-----------------------------------------------------------------------------------------------------------------
281
   Private Function ValidForIsNumeric ( fieldValue, rowId )
282
      ValidForIsNumeric = FALSE
283
      If maRules( InxIsNumeric, rowId ) = enumDB_YES Then
284
         If IsNumeric( fieldValue ) Then ValidForIsNumeric = TRUE
285
 
286
      Else
287
         ValidForIsNumeric = TRUE
288
 
289
      End If
290
   End Function
291
   '-----------------------------------------------------------------------------------------------------------------
292
   Private Function ValidForMinNumericValue ( fieldValue, rowId )
293
      ValidForMinNumericValue = FALSE
294
      If IsNull( maRules( InxMinNumericValue, rowId ) ) OR (maRules( InxMinNumericValue, rowId ) = "") Then
295
         ValidForMinNumericValue = TRUE
296
      Else
297
         If CDbl(fieldValue) >= CDbl(maRules( InxMinNumericValue, rowId )) Then   ValidForMinNumericValue = TRUE
298
 
299
      End If
300
   End Function
301
   '-----------------------------------------------------------------------------------------------------------------
302
   Private Function ValidForMaxNumericValue ( fieldValue, rowId )
303
      ValidForMaxNumericValue = FALSE
304
      If IsNull( maRules( InxMaxNumericValue, rowId ) ) OR (maRules( InxMaxNumericValue, rowId ) = "") Then
305
         ValidForMaxNumericValue = TRUE
306
      Else
307
         If CDbl(fieldValue) <= CDbl(maRules( InxMaxNumericValue, rowId )) Then   ValidForMaxNumericValue = TRUE
308
 
309
      End If
310
   End Function
311
   '-----------------------------------------------------------------------------------------------------------------
312
   Private Function ValidForIsDate ( fieldValue, rowId )
313
      ValidForIsDate = FALSE
314
      If maRules( InxIsDate, rowId ) = enumDB_YES Then
315
         If IsDate( fieldValue ) Then ValidForIsDate = TRUE
316
 
317
      Else
318
         ValidForIsDate = TRUE
319
 
320
      End If
321
   End Function
322
   '-----------------------------------------------------------------------------------------------------------------
323
   Private Function ValidForStartDate ( fieldValue, rowId )
324
      ValidForStartDate = FALSE
325
      If IsNull( maRules( InxStartDate, rowId ) ) OR (maRules( InxStartDate, rowId ) = "") Then
326
         ValidForStartDate = TRUE
327
      Else
328
         If CDate(fieldValue) >= CDate(maRules( InxStartDate, rowId )) Then   ValidForStartDate = TRUE
329
 
330
      End If
331
   End Function
332
   '-----------------------------------------------------------------------------------------------------------------
333
   Private Function ValidForEndDate ( fieldValue, rowId )
334
      ValidForEndDate = FALSE
335
      If IsNull( maRules( InxEndDate, rowId ) ) OR (maRules( InxEndDate, rowId ) = "") Then
336
         ValidForEndDate = TRUE
337
      Else
338
         If CDate(fieldValue) <= CDate(maRules( InxStartDate, rowId )) Then   ValidForEndDate = TRUE
339
 
340
      End If
341
   End Function
342
   '-----------------------------------------------------------------------------------------------------------------
343
   Private Function ValidForMinStringLength ( fieldValue, rowId )
344
      ValidForMinStringLength = FALSE
345
      If IsNull( maRules( InxMinStringLength, rowId ) ) OR (maRules( InxMinStringLength, rowId ) = "") Then
346
         ValidForMinStringLength = TRUE
347
      Else
348
         If CInt(Len(fieldValue)) >= CInt(maRules( InxMinStringLength, rowId )) Then   ValidForMinStringLength = TRUE
349
 
350
      End If
351
   End Function
352
   '-----------------------------------------------------------------------------------------------------------------
353
   Private Function ValidForMaxStringLength ( fieldValue, rowId )
354
      ValidForMaxStringLength = FALSE
355
 
356
      If IsNull( maRules( InxMaxStringLength, rowId ) ) OR (maRules( InxMaxStringLength, rowId ) = "") Then
357
         ValidForMaxStringLength = TRUE
358
      Else
359
         If CInt(Len(fieldValue)) <= CInt(maRules( InxMaxStringLength, rowId )) Then   ValidForMaxStringLength = TRUE
360
 
361
      End If
362
   End Function
363
   '-----------------------------------------------------------------------------------------------------------------
364
   Private Sub AddErrorMessage ( rowId, sErrMsg )
365
      mobjErrorMsg.Item (Cstr( maRules( InxFieldName, rowId ) )) = _
366
         mobjErrorMsg.Item (Cstr( maRules( InxFieldName, rowId ) )) &_
367
         "<tr>"&_
368
         sBULET  &"<td class='val_err'>"&  sErrMsg  &"</td>"&_
369
         "</tr>"& VBNewLine
370
   End Sub
371
   '-----------------------------------------------------------------------------------------------------------------
372
   Private Function LastRowInx ()
373
       LastRowInx = UBound ( maRules, 2 )
374
   End Function
375
   '-----------------------------------------------------------------------------------------------------------------
376
   Private Function GetErrorMsg ( sFieldName )
377
      Dim msg
378
      msg = mobjErrorMsg.Item (CStr(sFieldName))
379
      If msg <> "" Then
380
         GetErrorMsg = _
381
         "<table width='100%'  border='0' cellspacing='2' cellpadding='0'>"& VBNewLine &_
382
         msg & VBNewLine &_
383
         "<tr><td width='1'>"& SPACER &"</td><td width='1'>"& SPACER &"</td><td width='100%'>"& SPACER &"</td></tr>"& VBNewLine &_
384
         "</table>"
385
      Else
386
         GetErrorMsg = NULL
387
      End If
388
   End Function
389
   '-----------------------------------------------------------------------------------------------------------------
390
   Private Function ParseParams ( sParams )
391
      Dim paramArr, i
392
 
393
      paramArr = Split ( sParams, "'" )      ' Expected Params value:  id='field_name' IsRequired='N' param2='val' ...
394
 
395
      ' Store Validation changes/rules in array
396
      For i = 0 To UBound( paramArr )-1 Step 2
397
         Call UpdateRow ( paramArr(1), GetColumnInx ( paramArr(i) ), paramArr(i+1) )
398
      Next
399
 
400
      ParseParams = paramArr(1)   ' id must be first param
401
 
402
   End Function
403
   '-----------------------------------------------------------------------------------------------------------------
404
   Public Sub UpdateRules ( sParams )
405
      ParseParams ( sParams )
406
 
407
   End Sub
408
   '-----------------------------------------------------------------------------------------------------------------
409
   Private Sub UpdateRow ( sFieldName, sColumnInx, sColumnVal )
410
      Dim rowNum
411
 
412
      If mobjFieldMap.Exists (CStr(sFieldName)) Then
413
         rowNum = mobjFieldMap.Item ( Cstr( sFieldName  ) )
414
 
415
      Else
416
         rowNum = LastRowInx() + 1
417
         ReDim Preserve maRules( mNumOfRules, rowNum )
418
 
419
         mobjFieldMap.Add ( Cstr( sFieldName ) ), CStr( rowNum )
420
 
421
      End If
422
 
423
      maRules ( sColumnInx, rowNum ) = sColumnVal
424
 
425
   End Sub
426
   '-----------------------------------------------------------------------------------------------------------------
427
   Private Function GetColumnInx ( ByVal sParam )
428
      sParam = Trim( sParam )      ' Trim spaces
429
      sParam = Left( sParam, Len(sParam)-1 )   ' Remove trailing "="
430
 
431
      Select Case Trim( sParam )
432
         Case "id"
433
            GetColumnInx = InxFieldName
434
         Case Else
435
            GetColumnInx = Eval( "Inx"& Trim( sParam ) )
436
 
437
      End Select
438
   End Function
439
   '-----------------------------------------------------------------------------------------------------------------
440
   Private Sub Class_Initialize()
441
      '// Perform action on creation of object. e.g. Set myObj = New ThisClassName
442
      Set mobjFieldMap = CreateObject("Scripting.Dictionary")
443
      Set mobjErrorMsg = CreateObject("Scripting.Dictionary")
444
      Set mobjAltVal     = CreateObject("Scripting.Dictionary")
445
 
446
      sPostBackTagName = "VC_POST_BACK"
447
 
448
 
449
      bHiddenTagPlanted = FALSE
450
      bIsPostBack = FALSE      ' When true, form has been submitted and need postback validation
451
      bIsValidated = FALSE
452
 
453
 
5596 dpurdie 454
      InxFieldName          = 0
129 ghuddy 455
      InxIsRequired         = 1
5596 dpurdie 456
      InxIsNumeric          = 2
457
      InxMinNumericValue    = 3
458
      InxMaxNumericValue    = 4
459
      InxIsDate             = 5
460
      InxStartDate          = 6
129 ghuddy 461
      InxEndDate            = 7
5596 dpurdie 462
      InxMinStringLength    = 8
463
      InxMaxStringLength    = 9
464
      InxRegExp             = 10
465
      InxRegExpDescription  = 11
466
      mNumOfRules           = 12     ' Number of Rules that can be assigned to one field
129 ghuddy 467
      mLastRuleInx = mNumOfRules - 1
468
 
469
      ReDim maRules ( mNumOfRules, -1 )
470
 
471
      'sSESSION_SEPARATOR = "|SEPARATOR|"      ' Make sure it will never show in regexp field
4028 dpurdie 472
      sBULET = "<td>"& SPACER &"</td>"&_
129 ghuddy 473
             "<td valign='top'><img src='icons/i_bulet_red.gif' width='4' height='4' hspace='3' vspace='4' border='0' align='absmiddle'></td>"
474
 
475
 
476
      '--- Check if Form is posted back
477
      If Request(sPostBackTagName) <> "" Then
478
         bIsPostBack = TRUE
479
      End If
480
   End Sub
481
   '-----------------------------------------------------------------------------------------------------------------
482
   Private Sub Class_Terminate()
483
      '// Perform action on object disposal. e.g. Set myObj = Nothing
484
      Set mobjFieldMap = Nothing
485
      Set mobjErrorMsg = Nothing
486
      Set mobjAltVal = Nothing
487
 
488
   End Sub
489
   '-----------------------------------------------------------------------------------------------------------------
119 ghuddy 490
End Class
129 ghuddy 491
%>