Subversion Repositories DevTools

Rev

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