Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

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