Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
2 rsolanki 1
<%
2
'=============================================================
3
'//
4
'//						Validation Control
5
'//
6
'// version: 		1.4
7
'//	last modified: 	20-Jul-2004 11:26 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
			If fieldValue <> "" Then ValidForIsRequired = TRUE
236
 
237
		Else
238
			ValidForIsRequired = TRUE
239
 
240
		End If
241
	End Function
242
	'-----------------------------------------------------------------------------------------------------------------
243
	Private Function ValidForRegExp ( fieldValue, rowId )
244
		Dim objRegEx
245
 
246
		ValidForRegExp = FALSE
247
		If IsNull( maRules( InxRegExp, rowId ) ) OR (maRules( InxRegExp, rowId ) = "") Then 
248
			ValidForRegExp = TRUE
249
		Else
250
			Set objRegEx = New RegExp 
251
 
252
			objRegEx.Global = False		' Only find first match. This is enough to fail the validation
253
			objRegEx.IgnoreCase = False	' Follow match pattern exactly. 
254
    		objRegEx.Pattern = maRules( InxRegExp, rowId ) 		' Set the pattern to match
255
 
256
			' Now test the pattern match.
257
			If NOT objRegEx.Test( fieldValue ) Then
258
				ValidForRegExp = TRUE
259
			End If
260
 
261
			Set objRegEx = Nothing
262
		End If
263
	End Function
264
	'-----------------------------------------------------------------------------------------------------------------
265
	Private Function ValidForIsNumeric ( fieldValue, rowId )
266
		ValidForIsNumeric = FALSE
267
		If maRules( InxIsNumeric, rowId ) = enumDB_YES Then 
268
			If IsNumeric( fieldValue ) Then ValidForIsNumeric = TRUE
269
 
270
		Else
271
			ValidForIsNumeric = TRUE
272
 
273
		End If
274
	End Function
275
	'-----------------------------------------------------------------------------------------------------------------
276
	Private Function ValidForMinNumericValue ( fieldValue, rowId )
277
		ValidForMinNumericValue = FALSE
278
		If IsNull( maRules( InxMinNumericValue, rowId ) ) OR (maRules( InxMinNumericValue, rowId ) = "") Then 
279
			ValidForMinNumericValue = TRUE
280
		Else
281
			If CDbl(fieldValue) >= CDbl(maRules( InxMinNumericValue, rowId )) Then	ValidForMinNumericValue = TRUE
282
 
283
		End If
284
	End Function
285
	'-----------------------------------------------------------------------------------------------------------------
286
	Private Function ValidForMaxNumericValue ( fieldValue, rowId )
287
		ValidForMaxNumericValue = FALSE
288
		If IsNull( maRules( InxMaxNumericValue, rowId ) ) OR (maRules( InxMaxNumericValue, rowId ) = "") Then 	
289
			ValidForMaxNumericValue = TRUE
290
		Else
291
			If CDbl(fieldValue) <= CDbl(maRules( InxMaxNumericValue, rowId )) Then	ValidForMaxNumericValue = TRUE
292
 
293
		End If
294
	End Function
295
	'-----------------------------------------------------------------------------------------------------------------
296
	Private Function ValidForIsDate ( fieldValue, rowId )
297
		ValidForIsDate = FALSE
298
		If maRules( InxIsDate, rowId ) = enumDB_YES Then 
299
			If IsDate( fieldValue ) Then ValidForIsDate = TRUE
300
 
301
		Else
302
			ValidForIsDate = TRUE
303
 
304
		End If
305
	End Function
306
	'-----------------------------------------------------------------------------------------------------------------
307
	Private Function ValidForStartDate ( fieldValue, rowId )
308
		ValidForStartDate = FALSE
309
		If IsNull( maRules( InxStartDate, rowId ) ) OR (maRules( InxStartDate, rowId ) = "") Then 
310
			ValidForStartDate = TRUE
311
		Else
312
			If CDate(fieldValue) >= CDate(maRules( InxStartDate, rowId )) Then	ValidForStartDate = TRUE
313
 
314
		End If
315
	End Function
316
	'-----------------------------------------------------------------------------------------------------------------
317
	Private Function ValidForEndDate ( fieldValue, rowId )
318
		ValidForEndDate = FALSE
319
		If IsNull( maRules( InxEndDate, rowId ) ) OR (maRules( InxEndDate, rowId ) = "") Then 
320
			ValidForEndDate = TRUE
321
		Else
322
			If CDate(fieldValue) <= CDate(maRules( InxStartDate, rowId )) Then	ValidForEndDate = TRUE
323
 
324
		End If
325
	End Function
326
	'-----------------------------------------------------------------------------------------------------------------
327
	Private Function ValidForMinStringLength ( fieldValue, rowId )
328
		ValidForMinStringLength = FALSE
329
		If IsNull( maRules( InxMinStringLength, rowId ) ) OR (maRules( InxMinStringLength, rowId ) = "") Then 
330
			ValidForMinStringLength = TRUE
331
		Else
332
			If CInt(Len(fieldValue)) >= CInt(maRules( InxMinStringLength, rowId )) Then	ValidForMinStringLength = TRUE
333
 
334
		End If
335
	End Function
336
	'-----------------------------------------------------------------------------------------------------------------
337
	Private Function ValidForMaxStringLength ( fieldValue, rowId )
338
		ValidForMaxStringLength = FALSE
339
 
340
		If IsNull( maRules( InxMaxStringLength, rowId ) ) OR (maRules( InxMaxStringLength, rowId ) = "") Then 
341
			ValidForMaxStringLength = TRUE
342
		Else
343
			If CInt(Len(fieldValue)) <= CInt(maRules( InxMaxStringLength, rowId )) Then	ValidForMaxStringLength = TRUE
344
 
345
		End If
346
	End Function
347
	'-----------------------------------------------------------------------------------------------------------------
348
	Private Sub AddErrorMessage ( rowId, sErrMsg )
349
		mobjErrorMsg.Item (Cstr( maRules( InxFieldName, rowId ) )) = _
350
			mobjErrorMsg.Item (Cstr( maRules( InxFieldName, rowId ) )) &_
351
			"<tr>"&_
352
			sBULET  &"<td class='val_err'>"&  sErrMsg  &"</td>"&_
353
			"</tr>"& VBNewLine
354
	End Sub
355
	'-----------------------------------------------------------------------------------------------------------------
356
	Public Sub LoadFieldRules ( aRows )
357
		' Pass the full array row matching the columns of maRules()
358
		Dim nProperty, newArrayDim, numOfRows, rowNum
359
 
360
		numOfRows = UBound( aRows, 2 )
361
 
362
 
363
		For rowNum = 0 To numOfRows
364
			' Increase array by 1
365
			newArrayDim = LastRowInx() + 1
366
			ReDim Preserve maRules( mNumOfRules, newArrayDim )
367
 
368
			mobjFieldMap.Add ( Cstr( aRows ( InxFieldName, rowNum ) ) ), CStr( newArrayDim )
369
 
370
			For nProperty = 0 To mLastRuleInx
371
				maRules ( nProperty, newArrayDim ) = aRows ( nProperty, rowNum )
372
			Next
373
 
374
		Next
375
 
376
		' --- Validate Form ---
377
		'ValidateForm()
378
 
379
	End Sub
380
	'-----------------------------------------------------------------------------------------------------------------
381
	Private Function LastRowInx ()
382
		 LastRowInx = UBound ( maRules, 2 )
383
	End Function
384
	'-----------------------------------------------------------------------------------------------------------------
385
	Private Function GetErrorMsg ( sFieldName )
386
		Dim msg
387
		msg = mobjErrorMsg.Item (CStr(sFieldName))
388
		If msg <> "" Then
389
			GetErrorMsg = _
390
			"<table width='100%'  border='0' cellspacing='2' cellpadding='0'>"& VBNewLine &_
391
			msg & VBNewLine &_
392
			"<tr><td width='1'>"& SPACER &"</td><td width='1'>"& SPACER &"</td><td width='100%'>"& SPACER &"</td></tr>"& VBNewLine &_
393
			"</table>"
394
		Else
395
			GetErrorMsg = NULL
396
		End If
397
	End Function
398
	'-----------------------------------------------------------------------------------------------------------------
399
	Private Function ParseParams ( sParams )
400
		Dim paramArr, i
401
 
402
		paramArr = Split ( sParams, "'" )		' Expected Params value:  id='field_name' param1='val' param2='val' ...
403
 
404
		' Store Validation changes/rules in array
405
		For i = 0 To UBound( paramArr )-1 Step 2
406
			Call UpdateRow ( paramArr(1), GetColumnInx ( paramArr(i) ), paramArr(i+1) )
407
		Next
408
 
409
		ParseParams = paramArr(1)	' id must be first param
410
 
411
	End Function
412
	'-----------------------------------------------------------------------------------------------------------------
413
	Public Sub UpdateRules ( sParams )
414
		ParseParams ( sParams )
415
 
416
	End Sub
417
	'-----------------------------------------------------------------------------------------------------------------
418
	Private Sub UpdateRow ( sFieldName, sColumnInx, sColumnVal )
419
		Dim rowNum
420
 
421
		If mobjFieldMap.Exists (CStr(sFieldName)) Then
422
			rowNum = mobjFieldMap.Item ( Cstr( sFieldName  ) )
423
 
424
		Else
425
			rowNum = LastRowInx() + 1
426
			ReDim Preserve maRules( mNumOfRules, rowNum )
427
 
428
			mobjFieldMap.Add ( Cstr( sFieldName ) ), CStr( rowNum )
429
 
430
		End If
431
 
432
		maRules ( sColumnInx, rowNum ) = sColumnVal
433
 
434
	End Sub
435
	'-----------------------------------------------------------------------------------------------------------------
436
	Private Function GetColumnInx ( ByVal sParam )
437
		sParam = Trim( sParam )		' Trim spaces
438
		sParam = Left( sParam, Len(sParam)-1 )	' Remove trailing "="
439
 
440
		Select Case Trim( sParam )
441
			Case "id"
442
				GetColumnInx = InxFieldName
443
			Case Else
444
				GetColumnInx = Eval( "Inx"& Trim( sParam ) )
445
 
446
		End Select
447
	End Function
448
	'-----------------------------------------------------------------------------------------------------------------
449
	Sub LoadValidationRules ( aFieldList, ByRef objOraDatabase )
450
		Dim rsQry, query
451
 
452
		query = _
453
		"   SELECT FIELD_NAME, "&_
454
		"		   IS_REQUIRED, "&_
455
		"		   IS_NUMERIC, "&_
456
		"		   MIN_NUMERIC_VALUE, "&_
457
		"		   MAX_NUMERIC_VALUE, "&_
458
		"		   IS_DATE, "&_
459
		"		   START_DATE, "&_
460
		"		   END_DATE, "&_
461
		"		   MIN_STRING_LENGTH, "&_
462
		" 		   MAX_STRING_LENGTH, "&_
463
		"		   REGEXP, "&_
464
		"		   REGEXP_DESCRIPTION "&_
465
		"	  FROM VALIDATION_RULES"&_
466
		"	 WHERE field_name IN ( '"& Join( aFieldList, "','") &"' )"
467
 
468
 
469
		Set rsQry = objOraDatabase.DbCreateDynaset( query , 0 )
470
		If ((NOT rsQry.BOF) AND (NOT rsQry.EOF)) Then
471
			Call LoadFieldRules ( rsQry.GetRows() )
472
 
473
		End If
474
 
475
		rsQry.Close
476
		Set rsQry = Nothing
477
	End Sub
478
	'-----------------------------------------------------------------------------------------------------------------
479
	Private Sub Class_Initialize()
480
		'// Perform action on creation of object. e.g. Set myObj = New ThisClassName
481
		Set mobjFieldMap = CreateObject("Scripting.Dictionary")
482
		Set mobjErrorMsg = CreateObject("Scripting.Dictionary")
483
		Set mobjAltVal 	 = CreateObject("Scripting.Dictionary")
484
 
485
		sPostBackTagName = "VC_POST_BACK"
486
 
487
 
488
		bHiddenTagPlanted = FALSE
489
		bIsPostBack = FALSE		' When true, form has been submitted and need postback validation
490
		bIsValidated = FALSE
491
 
492
 
493
		InxFieldName			= 0
494
		InxIsRequired			= 1
495
		InxIsNumeric			= 2
496
		InxMinNumericValue		= 3
497
		InxMaxNumericValue		= 4
498
		InxIsDate				= 5
499
		InxStartDate			= 6
500
		InxEndDate				= 7
501
		InxMinStringLength		= 8
502
		InxMaxStringLength		= 9
503
		InxRegExp				= 10
504
		InxRegExpDescription	= 11
505
		mNumOfRules = 12  	' Number of Rules that can be assigned to one field
506
		mLastRuleInx = mNumOfRules - 1
507
 
508
		ReDim maRules ( mNumOfRules, -1 )
509
 
510
		'sSESSION_SEPARATOR = "|SEPARATOR|"		' Make sure it will never show in regexp field
511
		sBULET = "<td background='images/red_dot.gif'>"& SPACER &"</td>"&_
512
				 "<td valign='top'><img src='icons/i_bulet_red.gif' width='4' height='4' hspace='3' vspace='4' border='0' align='absmiddle'></td>"
513
 
514
 
515
		'--- Check if Form is posted back
516
		If Request(sPostBackTagName) <> "" Then
517
			bIsPostBack = TRUE
518
		End If
519
	End Sub
520
	'-----------------------------------------------------------------------------------------------------------------
521
	Private Sub Class_Terminate()
522
		'// Perform action on object disposal. e.g. Set myObj = Nothing
523
		Set mobjFieldMap = Nothing
524
		Set mobjErrorMsg = Nothing
525
		Set mobjAltVal = Nothing
526
 
527
	End Sub
528
	'-----------------------------------------------------------------------------------------------------------------
529
End Class
530
%>