Subversion Repositories DevTools

Rev

Rev 5840 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13 rsolanki 1
<%
2
'=============================================================
3
'//
5836 dpurdie 4
'//                  Validation Control
13 rsolanki 5
'//
6
'=============================================================
7
%>
8
<%
9
Class ValidationControl
5836 dpurdie 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
111
 
112
   '-----------------------------------------------------------------------------------------------------------------
113
   Public Function AddPostBack ()
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
120
   End Function
121
 
122
   '-----------------------------------------------------------------------------------------------------------------
123
   Public Function Validate ( sFieldName )
124
 
125
      '--- Plant Hidden Tag
126
      AddPostBack()
127
 
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
         Call ValidateField ( FieldValue, i )
143
 
144
      Next
145
 
146
      ' --- Finally, set the Form state of validity
147
      If mobjErrorMsg.Count > 0 Then
148
         bIsFormValid = FALSE
149
      Else
150
         bIsFormValid = TRUE
151
      End If
152
 
153
 
154
      bIsValidated = TRUE
155
      ValidateForm = bIsFormValid
156
 
157
   End Function
158
   '-----------------------------------------------------------------------------------------------------------------
159
   Private Sub ValidateField ( FieldValue, i )
160
 
161
      If (i = "") OR (IsNull(i)) Then
162
         Err.Raise 8, "Cannot Find Field Name.", "Make sure you have correct filed names listed for validation."
163
         Exit Sub
164
      End If
165
 
166
      If mobjErrorMsg.Exists ( CStr( maRules( InxFieldName, i ) ) ) Then mobjErrorMsg.Remove ( CStr( maRules( InxFieldName, i ) ) )   ' Clean this field Error Messages
167
 
168
      ' RULE is_Required
169
      If NOT ValidForIsRequired ( FieldValue, i ) Then
170
         Call AddErrorMessage ( i, "Required." )
171
 
172
      Else
173
 
174
         If FieldValue <> "" Then
175
 
176
            ' --- RULE is_Number ---
177
            If NOT ValidForIsNumeric ( FieldValue, i ) Then
178
               Call AddErrorMessage ( i, "Must be a number." )
179
            Else
180
 
181
               If maRules( InxIsNumeric, i ) = enumDB_YES Then  ' Continue if field is a Number
182
                  ' --- RULE min_Numeric_value ---
183
                  If NOT ValidForMinNumericValue ( FieldValue, i ) Then
184
                     Call AddErrorMessage ( i, "Must be minimum "& maRules( InxMinNumericValue, i ) &"." )
185
                  End If
186
 
187
                  ' --- RULE max_Numeric_value ---
188
                  If NOT ValidForMaxNumericValue ( FieldValue, i ) Then
189
                     Call AddErrorMessage ( i, "Must be maximum "& maRules( InxMaxNumericValue, i ) &"." )
190
                  End If
191
               End If
192
 
193
            End If
194
 
195
 
196
            ' --- RULE is_Date ---
197
            If NOT ValidForIsDate ( FieldValue, i ) Then
198
               Call AddErrorMessage ( i, "Must be a date." )
199
            Else
200
 
201
               If maRules( InxIsDate, i ) = enumDB_YES Then  ' Continue if field is a Date
202
                  ' --- RULE start_Date ---
203
                  If NOT ValidForStartDate ( FieldValue, i ) Then
204
                     Call AddErrorMessage ( i, "Cannot be before "& maRules( InxStartDate, i ) &"." )
205
                  End If
206
 
207
                  ' --- RULE end_Date ---
208
                  If NOT ValidForEndDate ( FieldValue, i ) Then
209
                     Call AddErrorMessage ( i, "Cannot be after "& maRules( InxStartDate, i ) &"." )
210
                  End If
211
 
212
               End If
213
 
214
            End If
215
 
216
 
217
            ' --- RULE min_String_Length ---
218
            If NOT ValidForMinStringLength ( FieldValue, i ) Then
219
               Call AddErrorMessage ( i, "Must be at least "& maRules( InxMinStringLength, i ) &" character(s) long." )
220
            End If
221
 
222
            ' --- RULE min_String_Length ---
223
            If NOT ValidForMaxStringLength ( FieldValue, i ) Then
224
               Call AddErrorMessage ( i, "Can be maximum "& maRules( InxMaxStringLength, i ) &" character(s) long." )
225
            End If
226
 
227
 
228
            ' --- RULE Regular Expression ---
229
            If NOT ValidForRegExp ( FieldValue, i ) Then
230
               Call AddErrorMessage ( i, maRules( InxRegExpDescription, i ) )
231
            End If
232
 
233
 
234
 
235
         End If
236
 
237
 
238
      End If
239
   End Sub
240
   '-----------------------------------------------------------------------------------------------------------------
241
   Private Function ValidForIsRequired ( fieldValue, rowId )
242
      ValidForIsRequired = FALSE
243
      If maRules( InxIsRequired, rowId ) = enumDB_YES Then
244
         '/* Check if empty */
245
         If fieldValue <> "" Then
246
 
247
            '/* Check for spaces */
248
            If Len( Replace(fieldValue, " ", "") ) > 0 Then   ValidForIsRequired = TRUE
249
 
250
         End If
251
 
252
      Else
253
         ValidForIsRequired = TRUE
254
 
255
      End If
256
   End Function
257
   '-----------------------------------------------------------------------------------------------------------------
258
   Private Function ValidForRegExp ( fieldValue, rowId )
259
      Dim objRegEx
260
 
261
      ValidForRegExp = FALSE
262
      If IsNull( maRules( InxRegExp, rowId ) ) OR (maRules( InxRegExp, rowId ) = "") Then
263
         ValidForRegExp = TRUE
264
      Else
265
         Set objRegEx = New RegExp
266
 
267
         objRegEx.Global = False      ' Only find first match. This is enough to fail the validation
268
         objRegEx.IgnoreCase = False   ' Follow match pattern exactly.
269
          objRegEx.Pattern = maRules( InxRegExp, rowId )       ' Set the pattern to match
270
 
271
         ' Now test the pattern match.
272
         If NOT objRegEx.Test( fieldValue ) Then
273
            ValidForRegExp = TRUE
274
         End If
275
 
276
         Set objRegEx = Nothing
277
      End If
278
   End Function
279
   '-----------------------------------------------------------------------------------------------------------------
280
   Private Function ValidForIsNumeric ( fieldValue, rowId )
281
      ValidForIsNumeric = FALSE
282
      If maRules( InxIsNumeric, rowId ) = enumDB_YES Then
283
         If IsNumeric( fieldValue ) Then ValidForIsNumeric = TRUE
284
 
285
      Else
286
         ValidForIsNumeric = TRUE
287
 
288
      End If
289
   End Function
290
   '-----------------------------------------------------------------------------------------------------------------
291
   Private Function ValidForMinNumericValue ( fieldValue, rowId )
292
      ValidForMinNumericValue = FALSE
293
      If IsNull( maRules( InxMinNumericValue, rowId ) ) OR (maRules( InxMinNumericValue, rowId ) = "") Then
294
         ValidForMinNumericValue = TRUE
295
      Else
296
         If CDbl(fieldValue) >= CDbl(maRules( InxMinNumericValue, rowId )) Then   ValidForMinNumericValue = TRUE
297
 
298
      End If
299
   End Function
300
   '-----------------------------------------------------------------------------------------------------------------
301
   Private Function ValidForMaxNumericValue ( fieldValue, rowId )
302
      ValidForMaxNumericValue = FALSE
303
      If IsNull( maRules( InxMaxNumericValue, rowId ) ) OR (maRules( InxMaxNumericValue, rowId ) = "") Then
304
         ValidForMaxNumericValue = TRUE
305
      Else
306
         If CDbl(fieldValue) <= CDbl(maRules( InxMaxNumericValue, rowId )) Then   ValidForMaxNumericValue = TRUE
307
 
308
      End If
309
   End Function
310
   '-----------------------------------------------------------------------------------------------------------------
311
   Private Function ValidForIsDate ( fieldValue, rowId )
312
      ValidForIsDate = FALSE
313
      If maRules( InxIsDate, rowId ) = enumDB_YES Then
314
         If IsDate( fieldValue ) Then ValidForIsDate = TRUE
315
 
316
      Else
317
         ValidForIsDate = TRUE
318
 
319
      End If
320
   End Function
321
   '-----------------------------------------------------------------------------------------------------------------
322
   Private Function ValidForStartDate ( fieldValue, rowId )
323
      ValidForStartDate = FALSE
324
      If IsNull( maRules( InxStartDate, rowId ) ) OR (maRules( InxStartDate, rowId ) = "") Then
325
         ValidForStartDate = TRUE
326
      Else
327
         If CDate(fieldValue) >= CDate(maRules( InxStartDate, rowId )) Then   ValidForStartDate = TRUE
328
 
329
      End If
330
   End Function
331
   '-----------------------------------------------------------------------------------------------------------------
332
   Private Function ValidForEndDate ( fieldValue, rowId )
333
      ValidForEndDate = FALSE
334
      If IsNull( maRules( InxEndDate, rowId ) ) OR (maRules( InxEndDate, rowId ) = "") Then
335
         ValidForEndDate = TRUE
336
      Else
337
         If CDate(fieldValue) <= CDate(maRules( InxStartDate, rowId )) Then   ValidForEndDate = TRUE
338
 
339
      End If
340
   End Function
341
   '-----------------------------------------------------------------------------------------------------------------
342
   Private Function ValidForMinStringLength ( fieldValue, rowId )
343
      ValidForMinStringLength = FALSE
344
      If IsNull( maRules( InxMinStringLength, rowId ) ) OR (maRules( InxMinStringLength, rowId ) = "") Then
345
         ValidForMinStringLength = TRUE
346
      Else
347
         If CInt(Len(fieldValue)) >= CInt(maRules( InxMinStringLength, rowId )) Then   ValidForMinStringLength = TRUE
348
 
349
      End If
350
   End Function
351
   '-----------------------------------------------------------------------------------------------------------------
352
   Private Function ValidForMaxStringLength ( fieldValue, rowId )
353
      ValidForMaxStringLength = FALSE
354
 
355
      If IsNull( maRules( InxMaxStringLength, rowId ) ) OR (maRules( InxMaxStringLength, rowId ) = "") Then
356
         ValidForMaxStringLength = TRUE
357
      Else
358
         If CInt(Len(fieldValue)) <= CInt(maRules( InxMaxStringLength, rowId )) Then   ValidForMaxStringLength = TRUE
359
 
360
      End If
361
   End Function
362
   '-----------------------------------------------------------------------------------------------------------------
363
   Private Sub AddErrorMessage ( rowId, sErrMsg )
364
      mobjErrorMsg.Item (Cstr( maRules( InxFieldName, rowId ) )) = _
365
         mobjErrorMsg.Item (Cstr( maRules( InxFieldName, rowId ) )) &_
366
         "<tr>"&_
367
         sBULET  &"<td class='val_err'>"&  sErrMsg  &"</td>"&_
368
         "</tr>"& VBNewLine
369
   End Sub
370
   '-----------------------------------------------------------------------------------------------------------------
371
   Private Function LastRowInx ()
372
       LastRowInx = UBound ( maRules, 2 )
373
   End Function
374
   '-----------------------------------------------------------------------------------------------------------------
375
   Private Function GetErrorMsg ( sFieldName )
376
      Dim msg
377
      msg = mobjErrorMsg.Item (CStr(sFieldName))
378
      If msg <> "" Then
379
         GetErrorMsg = _
380
         "<table width='100%'  border='0' cellspacing='2' cellpadding='0'>"& VBNewLine &_
381
         msg & VBNewLine &_
382
         "<tr><td width='1'>"& SPACER &"</td><td width='1'>"& SPACER &"</td><td width='100%'>"& SPACER &"</td></tr>"& VBNewLine &_
383
         "</table>"
384
      Else
385
         GetErrorMsg = NULL
386
      End If
387
   End Function
388
   '-----------------------------------------------------------------------------------------------------------------
389
   Private Function ParseParams ( sParams )
390
      Dim paramArr, i
391
 
392
      paramArr = Split ( sParams, "'" )      ' Expected Params value:  id='field_name' IsRequired='N' param2='val' ...
393
 
394
      ' Store Validation changes/rules in array
395
      For i = 0 To UBound( paramArr )-1 Step 2
396
         Call UpdateRow ( paramArr(1), GetColumnInx ( paramArr(i) ), paramArr(i+1) )
397
      Next
398
 
399
      ParseParams = paramArr(1)   ' id must be first param
400
 
401
   End Function
402
   '-----------------------------------------------------------------------------------------------------------------
403
   Public Sub UpdateRules ( sParams )
404
      ParseParams ( sParams )
405
 
406
   End Sub
407
   '-----------------------------------------------------------------------------------------------------------------
408
   Private Sub UpdateRow ( sFieldName, sColumnInx, sColumnVal )
409
      Dim rowNum
410
 
411
      If mobjFieldMap.Exists (CStr(sFieldName)) Then
412
         rowNum = mobjFieldMap.Item ( Cstr( sFieldName  ) )
413
 
414
      Else
415
         rowNum = LastRowInx() + 1
416
         ReDim Preserve maRules( mNumOfRules, rowNum )
417
 
418
         mobjFieldMap.Add ( Cstr( sFieldName ) ), CStr( rowNum )
419
 
420
      End If
421
 
422
      maRules ( sColumnInx, rowNum ) = sColumnVal
423
 
424
   End Sub
425
   '-----------------------------------------------------------------------------------------------------------------
426
   Private Function GetColumnInx ( ByVal sParam )
427
      sParam = Trim( sParam )      ' Trim spaces
428
      sParam = Left( sParam, Len(sParam)-1 )   ' Remove trailing "="
429
 
430
      Select Case Trim( sParam )
431
         Case "id"
432
            GetColumnInx = InxFieldName
433
         Case Else
434
            GetColumnInx = Eval( "Inx"& Trim( sParam ) )
435
 
436
      End Select
437
   End Function
438
   '-----------------------------------------------------------------------------------------------------------------
5840 dpurdie 439
   ' Add a Rule, only if its required
440
   Private Sub addRule (aFieldList, sId, sRule)
441
      Dim sItem
442
      For Each sItem in aFieldList
443
         If 0 = StrComp(sItem, sId) Then
444
                UpdateRules (sRule)
445
                Exit Sub
446
         End If
447
      Next
448
   End Sub
449
   '-----------------------------------------------------------------------------------------------------------------
13 rsolanki 450
	Sub LoadValidationRules ( aFieldList, ByRef objOraDatabase )
5924 dpurdie 451
            Call addRule (aFieldList,"node_name","id='node_name' IsRequired='Y' MinStringLength='' MaxStringLength='100' RegExp='' RegExpDescription=''")
5840 dpurdie 452
            Call addRule (aFieldList,"node_comments","id='node_comments' IsRequired='N' MinStringLength='' MaxStringLength='4000' RegExp='' RegExpDescription=''")
453
            Call addRule (aFieldList,"os_name","id='os_name' IsRequired='Y' MinStringLength='' MaxStringLength='50' RegExp='' RegExpDescription=''")
454
            Call addRule (aFieldList,"os_comments","id='os_comments' IsRequired='N' MinStringLength='' MaxStringLength='4000' RegExp='' RegExpDescription=''")
455
            Call addRule (aFieldList,"branch_name","id='branch_name' IsRequired='Y' MinStringLength='' MaxStringLength='50' RegExp='' RegExpDescription=''")
456
            Call addRule (aFieldList,"proj_name","id='proj_name' IsRequired='Y' MaxStringLength='50'")
457
            Call addRule (aFieldList,"state_name","id='state_name' IsRequired='Y' MinStringLength='' MaxStringLength='50' RegExp='' RegExpDescription=''")
458
            Call addRule (aFieldList,"bom_name","id='bom_name' IsRequired='Y' MinStringLength='' MaxStringLength='50' RegExp='' RegExpDescription=''")
459
            Call addRule (aFieldList,"bom_comments","id='bom_comments' IsRequired='N' MinStringLength='' MaxStringLength='4000' RegExp='' RegExpDescription=''")
460
            Call addRule (aFieldList,"reject_note","id='reject_note' IsRequired='N' MinStringLength='' MaxStringLength='4000' RegExp='' RegExpDescription=''")
461
            Call addRule (aFieldList,"release_to_comments","id='release_to_comments' IsRequired='N' MinStringLength='' MaxStringLength='4000' RegExp='' RegExpDescription=''")
462
            Call addRule (aFieldList,"product_comments","id='product_comments' IsRequired='N' MinStringLength='' MaxStringLength='4000' RegExp='' RegExpDescription=''")
463
            Call addRule (aFieldList,"base_env_name","id='base_env_name' IsRequired='Y' MinStringLength='' MaxStringLength='50' RegExp='' RegExpDescription=''")
464
            Call addRule (aFieldList,"prod_name","id='prod_name' IsRequired='Y' MinStringLength='' MaxStringLength='255' RegExp='[^a-zA-Z0-9\.\!\$\-\_]' RegExpDescription='Allowed characters are <br>A-Z a-z 0-9 . ! $ - _'")
465
            Call addRule (aFieldList,"prod_version","id='prod_version' IsRequired='Y' MinStringLength='' MaxStringLength='50' RegExp='[^a-zA-Z0-9\.\!\$\-\_]' RegExpDescription='Allowed characters are <br>A-Z a-z 0-9 . ! $ - _'")
466
            Call addRule (aFieldList,"branch_comments","id='branch_comments' IsRequired='N' MinStringLength='' MaxStringLength='4000' RegExp='' RegExpDescription=''")
467
            Call addRule (aFieldList,"bom_lifecycle","id='bom_lifecycle' IsRequired='Y' MinStringLength='1' MaxStringLength='10' RegExp='[^0-9]' RegExpDescription='Only numbers accepted.'")
468
            Call addRule (aFieldList,"bom_version","id='bom_version' IsRequired='Y' MinStringLength='1' MaxStringLength='10' RegExp='[^a-zA-Z0-9\.\!\$\-\_]' RegExpDescription='Allowed characters are <br>A-Z a-z 0-9 . ! $ - _'")
5836 dpurdie 469
    End Sub
470
   '-----------------------------------------------------------------------------------------------------------------
471
   Private Sub Class_Initialize()
472
      '// Perform action on creation of object. e.g. Set myObj = New ThisClassName
473
      Set mobjFieldMap = CreateObject("Scripting.Dictionary")
474
      Set mobjErrorMsg = CreateObject("Scripting.Dictionary")
475
      Set mobjAltVal     = CreateObject("Scripting.Dictionary")
476
 
477
      sPostBackTagName = "VC_POST_BACK"
478
 
479
 
480
      bHiddenTagPlanted = FALSE
481
      bIsPostBack = FALSE      ' When true, form has been submitted and need postback validation
482
      bIsValidated = FALSE
483
 
484
 
485
      InxFieldName          = 0
486
      InxIsRequired         = 1
487
      InxIsNumeric          = 2
488
      InxMinNumericValue    = 3
489
      InxMaxNumericValue    = 4
490
      InxIsDate             = 5
491
      InxStartDate          = 6
492
      InxEndDate            = 7
493
      InxMinStringLength    = 8
494
      InxMaxStringLength    = 9
495
      InxRegExp             = 10
496
      InxRegExpDescription  = 11
497
      mNumOfRules           = 12     ' Number of Rules that can be assigned to one field
498
      mLastRuleInx = mNumOfRules - 1
499
 
500
      ReDim maRules ( mNumOfRules, -1 )
501
 
502
      'sSESSION_SEPARATOR = "|SEPARATOR|"      ' Make sure it will never show in regexp field
13 rsolanki 503
		sBULET = "<td background='images/red_dot.gif'>"& SPACER &"</td>"&_
5836 dpurdie 504
             "<td valign='top'><img src='icons/i_bulet_red.gif' width='4' height='4' hspace='3' vspace='4' border='0' align='absmiddle'></td>"
505
 
506
 
507
      '--- Check if Form is posted back
508
      If Request(sPostBackTagName) <> "" Then
509
         bIsPostBack = TRUE
510
      End If
511
   End Sub
512
   '-----------------------------------------------------------------------------------------------------------------
513
   Private Sub Class_Terminate()
514
      '// Perform action on object disposal. e.g. Set myObj = Nothing
515
      Set mobjFieldMap = Nothing
516
      Set mobjErrorMsg = Nothing
517
      Set mobjAltVal = Nothing
518
 
519
   End Sub
520
   '-----------------------------------------------------------------------------------------------------------------
13 rsolanki 521
End Class
5836 dpurdie 522
%>