Rev 13 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<%'============================================================='//'// Validation Control'//'// version: 1.6'// last modified: 02-Sep-2004 12:11 by Sasha Vukovic'=============================================================%><%Class ValidationControlPrivate maRules()Private mobjFieldMapPrivate mobjErrorMsgPrivate mobjAltValPrivate InxFieldNamePrivate InxIsRequiredPrivate InxIsNumericPrivate InxMinNumericValuePrivate InxMaxNumericValuePrivate InxIsDatePrivate InxStartDatePrivate InxEndDatePrivate InxMinStringLengthPrivate InxMaxStringLengthPrivate InxRegExpPrivate InxRegExpDescriptionPrivate mNumOfRulesPrivate mLastRuleInxPrivate bIsFormValidPrivate bIsValidatedPrivate bIsPostBackPrivate bHiddenTagPlantedPrivate sPostBackTagNamePrivate sBULETPublic Property Get IsPostBack ()IsPostBack = bIsPostBackEnd PropertyPublic Property Get IsValidOnPostBackIf bIsValidated ThenIsValidOnPostBack = bIsFormValidElseIsValidOnPostBack = ValidateForm()End IfEnd Property'-----------------------------------------------------------------------------------------------------------------Public Function GetValue ( sFieldName, altValue )If NOT bIsPostBack ThenGetValue = altValueElseGetValue = Request(sFieldName)End IfmobjAltVal.Item ( sFieldName ) = GetValueEnd Function'-----------------------------------------------------------------------------------------------------------------Public Sub SetValue ( sFieldName, altValue )If NOT bIsPostBack ThenmobjAltVal.Item ( sFieldName ) = altValueElsemobjAltVal.Item ( sFieldName ) = Request(sFieldName)End IfEnd Sub'-----------------------------------------------------------------------------------------------------------------Private Function RequestValue( sFieldName )If NOT bIsPostBack ThenRequestValue = mobjAltVal.Item ( sFieldName )ElseIf Request(sFieldName) <> "" ThenRequestValue = Request(sFieldName)ElseRequestValue = mobjAltVal.Item ( sFieldName )End IfEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Public Function IsTicked( sFieldName, nParId, altValue )Dim sParListnParId = ","& Replace(nParId, " ", "") &","sParList = ","& Replace( Request(sFieldName), " ", "") &","If NOT bIsPostBack ThenIsTicked = (NOT IsNull(altValue)) OR (altValue <> "")ElseIf InStr( sParList, nParId ) > 0 ThenIsTicked = TRUEElseIsTicked = FALSEEnd IfEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Public Function Validate ( sFieldName )'--- Plant Hidden TagIf NOT bHiddenTagPlanted Then' This tag is used by this class to know if the form is posted backResponse.write "<input type='hidden' name='"& sPostBackTagName &"' value='true'>"bHiddenTagPlanted = TRUEEnd IfCall ValidateField ( RequestValue(sFieldName), mobjFieldMap.Item ( Cstr( sFieldName ) ) )Validate = GetErrorMsg ( sFieldName )End Function'-----------------------------------------------------------------------------------------------------------------Private Function ValidateForm()Dim i, FieldValue, nLastRowInxmobjErrorMsg.RemoveAll ' Clean Error MessagesnLastRowInx = LastRowInx ()For i = 0 To nLastRowInxFieldValue = RequestValue( maRules( InxFieldName, i ) )Call ValidateField ( FieldValue, i )Next' --- Finally, set the Form state of validityIf mobjErrorMsg.Count > 0 ThenbIsFormValid = FALSEElsebIsFormValid = TRUEEnd IfbIsValidated = TRUEValidateForm = bIsFormValidEnd Function'-----------------------------------------------------------------------------------------------------------------Private Sub ValidateField ( FieldValue, i )If (i = "") OR (IsNull(i)) ThenErr.Raise 8, "Cannot Find Field Name.", "Make sure you have correct filed names listed for validation."Exit SubEnd IfIf mobjErrorMsg.Exists ( CStr( maRules( InxFieldName, i ) ) ) Then mobjErrorMsg.Remove ( CStr( maRules( InxFieldName, i ) ) ) ' Clean this field Error Messages' RULE is_RequiredIf NOT ValidForIsRequired ( FieldValue, i ) ThenCall AddErrorMessage ( i, "Required." )ElseIf FieldValue <> "" Then' --- RULE is_Number ---If NOT ValidForIsNumeric ( FieldValue, i ) ThenCall AddErrorMessage ( i, "Must be a number." )ElseIf maRules( InxIsNumeric, i ) = enumDB_YES Then ' Continue if field is a Number' --- RULE min_Numeric_value ---If NOT ValidForMinNumericValue ( FieldValue, i ) ThenCall AddErrorMessage ( i, "Must be minimum "& maRules( InxMinNumericValue, i ) &"." )End If' --- RULE max_Numeric_value ---If NOT ValidForMaxNumericValue ( FieldValue, i ) ThenCall AddErrorMessage ( i, "Must be maximum "& maRules( InxMaxNumericValue, i ) &"." )End IfEnd IfEnd If' --- RULE is_Date ---If NOT ValidForIsDate ( FieldValue, i ) ThenCall AddErrorMessage ( i, "Must be a date." )ElseIf maRules( InxIsDate, i ) = enumDB_YES Then ' Continue if field is a Date' --- RULE start_Date ---If NOT ValidForStartDate ( FieldValue, i ) ThenCall AddErrorMessage ( i, "Cannot be before "& maRules( InxStartDate, i ) &"." )End If' --- RULE end_Date ---If NOT ValidForEndDate ( FieldValue, i ) ThenCall AddErrorMessage ( i, "Cannot be after "& maRules( InxStartDate, i ) &"." )End IfEnd IfEnd If' --- RULE min_String_Length ---If NOT ValidForMinStringLength ( FieldValue, i ) ThenCall AddErrorMessage ( i, "Must be at least "& maRules( InxMinStringLength, i ) &" character(s) long." )End If' --- RULE min_String_Length ---If NOT ValidForMaxStringLength ( FieldValue, i ) ThenCall AddErrorMessage ( i, "Can be maximum "& maRules( InxMaxStringLength, i ) &" character(s) long." )End If' --- RULE Regular Expression ---If NOT ValidForRegExp ( FieldValue, i ) ThenCall AddErrorMessage ( i, maRules( InxRegExpDescription, i ) )End IfEnd IfEnd IfEnd Sub'-----------------------------------------------------------------------------------------------------------------Private Function ValidForIsRequired ( fieldValue, rowId )ValidForIsRequired = FALSEIf maRules( InxIsRequired, rowId ) = enumDB_YES Then'/* Check if empty */If fieldValue <> "" Then'/* Check for spaces */If Len( Replace(fieldValue, " ", "") ) > 0 Then ValidForIsRequired = TRUEEnd IfElseValidForIsRequired = TRUEEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Private Function ValidForRegExp ( fieldValue, rowId )Dim objRegExValidForRegExp = FALSEIf IsNull( maRules( InxRegExp, rowId ) ) OR (maRules( InxRegExp, rowId ) = "") ThenValidForRegExp = TRUEElseSet objRegEx = New RegExpobjRegEx.Global = False ' Only find first match. This is enough to fail the validationobjRegEx.IgnoreCase = False ' Follow match pattern exactly.objRegEx.Pattern = maRules( InxRegExp, rowId ) ' Set the pattern to match' Now test the pattern match.If NOT objRegEx.Test( fieldValue ) ThenValidForRegExp = TRUEEnd IfSet objRegEx = NothingEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Private Function ValidForIsNumeric ( fieldValue, rowId )ValidForIsNumeric = FALSEIf maRules( InxIsNumeric, rowId ) = enumDB_YES ThenIf IsNumeric( fieldValue ) Then ValidForIsNumeric = TRUEElseValidForIsNumeric = TRUEEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Private Function ValidForMinNumericValue ( fieldValue, rowId )ValidForMinNumericValue = FALSEIf IsNull( maRules( InxMinNumericValue, rowId ) ) OR (maRules( InxMinNumericValue, rowId ) = "") ThenValidForMinNumericValue = TRUEElseIf CDbl(fieldValue) >= CDbl(maRules( InxMinNumericValue, rowId )) Then ValidForMinNumericValue = TRUEEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Private Function ValidForMaxNumericValue ( fieldValue, rowId )ValidForMaxNumericValue = FALSEIf IsNull( maRules( InxMaxNumericValue, rowId ) ) OR (maRules( InxMaxNumericValue, rowId ) = "") ThenValidForMaxNumericValue = TRUEElseIf CDbl(fieldValue) <= CDbl(maRules( InxMaxNumericValue, rowId )) Then ValidForMaxNumericValue = TRUEEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Private Function ValidForIsDate ( fieldValue, rowId )ValidForIsDate = FALSEIf maRules( InxIsDate, rowId ) = enumDB_YES ThenIf IsDate( fieldValue ) Then ValidForIsDate = TRUEElseValidForIsDate = TRUEEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Private Function ValidForStartDate ( fieldValue, rowId )ValidForStartDate = FALSEIf IsNull( maRules( InxStartDate, rowId ) ) OR (maRules( InxStartDate, rowId ) = "") ThenValidForStartDate = TRUEElseIf CDate(fieldValue) >= CDate(maRules( InxStartDate, rowId )) Then ValidForStartDate = TRUEEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Private Function ValidForEndDate ( fieldValue, rowId )ValidForEndDate = FALSEIf IsNull( maRules( InxEndDate, rowId ) ) OR (maRules( InxEndDate, rowId ) = "") ThenValidForEndDate = TRUEElseIf CDate(fieldValue) <= CDate(maRules( InxStartDate, rowId )) Then ValidForEndDate = TRUEEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Private Function ValidForMinStringLength ( fieldValue, rowId )ValidForMinStringLength = FALSEIf IsNull( maRules( InxMinStringLength, rowId ) ) OR (maRules( InxMinStringLength, rowId ) = "") ThenValidForMinStringLength = TRUEElseIf CInt(Len(fieldValue)) >= CInt(maRules( InxMinStringLength, rowId )) Then ValidForMinStringLength = TRUEEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Private Function ValidForMaxStringLength ( fieldValue, rowId )ValidForMaxStringLength = FALSEIf IsNull( maRules( InxMaxStringLength, rowId ) ) OR (maRules( InxMaxStringLength, rowId ) = "") ThenValidForMaxStringLength = TRUEElseIf CInt(Len(fieldValue)) <= CInt(maRules( InxMaxStringLength, rowId )) Then ValidForMaxStringLength = TRUEEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Private Sub AddErrorMessage ( rowId, sErrMsg )mobjErrorMsg.Item (Cstr( maRules( InxFieldName, rowId ) )) = _mobjErrorMsg.Item (Cstr( maRules( InxFieldName, rowId ) )) &_"<tr>"&_sBULET &"<td class='val_err'>"& sErrMsg &"</td>"&_"</tr>"& VBNewLineEnd Sub'-----------------------------------------------------------------------------------------------------------------Public Sub LoadFieldRules ( aRows )' Pass the full array row matching the columns of maRules()Dim nProperty, newArrayDim, numOfRows, rowNumnumOfRows = UBound( aRows, 2 )For rowNum = 0 To numOfRows' Increase array by 1newArrayDim = LastRowInx() + 1ReDim Preserve maRules( mNumOfRules, newArrayDim )mobjFieldMap.Add ( Cstr( aRows ( InxFieldName, rowNum ) ) ), CStr( newArrayDim )For nProperty = 0 To mLastRuleInxmaRules ( nProperty, newArrayDim ) = aRows ( nProperty, rowNum )NextNext' --- Validate Form ---'ValidateForm()End Sub'-----------------------------------------------------------------------------------------------------------------Private Function LastRowInx ()LastRowInx = UBound ( maRules, 2 )End Function'-----------------------------------------------------------------------------------------------------------------Private Function GetErrorMsg ( sFieldName )Dim msgmsg = mobjErrorMsg.Item (CStr(sFieldName))If msg <> "" ThenGetErrorMsg = _"<table width='100%' border='0' cellspacing='2' cellpadding='0'>"& VBNewLine &_msg & VBNewLine &_"<tr><td width='1'>"& SPACER &"</td><td width='1'>"& SPACER &"</td><td width='100%'>"& SPACER &"</td></tr>"& VBNewLine &_"</table>"ElseGetErrorMsg = NULLEnd IfEnd Function'-----------------------------------------------------------------------------------------------------------------Private Function ParseParams ( sParams )Dim paramArr, iparamArr = Split ( sParams, "'" ) ' Expected Params value: id='field_name' IsRequired='N' param2='val' ...' Store Validation changes/rules in arrayFor i = 0 To UBound( paramArr )-1 Step 2Call UpdateRow ( paramArr(1), GetColumnInx ( paramArr(i) ), paramArr(i+1) )NextParseParams = paramArr(1) ' id must be first paramEnd Function'-----------------------------------------------------------------------------------------------------------------Public Sub UpdateRules ( sParams )ParseParams ( sParams )End Sub'-----------------------------------------------------------------------------------------------------------------Private Sub UpdateRow ( sFieldName, sColumnInx, sColumnVal )Dim rowNumIf mobjFieldMap.Exists (CStr(sFieldName)) ThenrowNum = mobjFieldMap.Item ( Cstr( sFieldName ) )ElserowNum = LastRowInx() + 1ReDim Preserve maRules( mNumOfRules, rowNum )mobjFieldMap.Add ( Cstr( sFieldName ) ), CStr( rowNum )End IfmaRules ( sColumnInx, rowNum ) = sColumnValEnd Sub'-----------------------------------------------------------------------------------------------------------------Private Function GetColumnInx ( ByVal sParam )sParam = Trim( sParam ) ' Trim spacessParam = Left( sParam, Len(sParam)-1 ) ' Remove trailing "="Select Case Trim( sParam )Case "id"GetColumnInx = InxFieldNameCase ElseGetColumnInx = Eval( "Inx"& Trim( sParam ) )End SelectEnd Function'-----------------------------------------------------------------------------------------------------------------Sub LoadValidationRules ( aFieldList, ByRef objOraDatabase )Dim rsQry, queryquery = _" SELECT FIELD_NAME, "&_" IS_REQUIRED, "&_" IS_NUMERIC, "&_" MIN_NUMERIC_VALUE, "&_" MAX_NUMERIC_VALUE, "&_" IS_DATE, "&_" START_DATE, "&_" END_DATE, "&_" MIN_STRING_LENGTH, "&_" MAX_STRING_LENGTH, "&_" REGEXP, "&_" REGEXP_DESCRIPTION "&_" FROM VALIDATION_RULES"&_" WHERE field_name IN ( '"& Join( aFieldList, "','") &"' )"Set rsQry = objOraDatabase.DbCreateDynaset( query , 0 )If ((NOT rsQry.BOF) AND (NOT rsQry.EOF)) ThenCall LoadFieldRules ( rsQry.GetRows() )End IfrsQry.CloseSet rsQry = NothingEnd Sub'-----------------------------------------------------------------------------------------------------------------Private Sub Class_Initialize()'// Perform action on creation of object. e.g. Set myObj = New ThisClassNameSet mobjFieldMap = CreateObject("Scripting.Dictionary")Set mobjErrorMsg = CreateObject("Scripting.Dictionary")Set mobjAltVal = CreateObject("Scripting.Dictionary")sPostBackTagName = "VC_POST_BACK"bHiddenTagPlanted = FALSEbIsPostBack = FALSE ' When true, form has been submitted and need postback validationbIsValidated = FALSEInxFieldName = 0InxIsRequired = 1InxIsNumeric = 2InxMinNumericValue = 3InxMaxNumericValue = 4InxIsDate = 5InxStartDate = 6InxEndDate = 7InxMinStringLength = 8InxMaxStringLength = 9InxRegExp = 10InxRegExpDescription = 11mNumOfRules = 12 ' Number of Rules that can be assigned to one fieldmLastRuleInx = mNumOfRules - 1ReDim maRules ( mNumOfRules, -1 )'sSESSION_SEPARATOR = "|SEPARATOR|" ' Make sure it will never show in regexp fieldsBULET = "<td background='images/red_dot.gif'>"& SPACER &"</td>"&_"<td valign='top'><img src='icons/i_bulet_red.gif' width='4' height='4' hspace='3' vspace='4' border='0' align='absmiddle'></td>"'--- Check if Form is posted backIf Request(sPostBackTagName) <> "" ThenbIsPostBack = TRUEEnd IfEnd Sub'-----------------------------------------------------------------------------------------------------------------Private Sub Class_Terminate()'// Perform action on object disposal. e.g. Set myObj = NothingSet mobjFieldMap = NothingSet mobjErrorMsg = NothingSet mobjAltVal = NothingEnd Sub'-----------------------------------------------------------------------------------------------------------------End Class%>