Rev 5838 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<%'============================================================='//'// Validation Control'//'=============================================================%><%Class ValidationControlPrivate maRules()Private mobjFieldMapPrivate mobjErrorMsgPrivate mobjAltValPrivate InxFieldNamePrivate InxIsRequiredPrivate InxIsNumericPrivate InxMinNumericValuePrivate InxMaxNumericValuePrivate InxIsDatePrivate InxStartDatePrivate InxEndDatePrivate InxMinStringLengthPrivate InxMaxStringLengthPrivate InxRegExpPrivate InxRegExpDescriptionPrivate InxIsNamePrivate 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'-----------------------------------------------------------------------------------------------------------------Public Sub SetValueForced ( sFieldName, value )mobjAltVal.Item ( sFieldName ) = valueEnd 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 AddPostBack ()'--- 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 IfEnd Function'-----------------------------------------------------------------------------------------------------------------Public Function Validate ( sFieldName )'--- Plant Hidden TagAddPostBack()Call 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 If' -- RULE for NameCall ValidValidForName ( FieldValue, i)End 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 objRegEx.Test( fieldValue ) ThenValidForRegExp = TRUEEnd If'Err.Raise 8, "Just a Test", "I got here4." & maRules( InxRegExp, rowId ) & ":" & fieldValue & "::" & ValidForRegExpSet 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 Function ValidValidForName ( fieldValue, rowId )ValidValidForName = TRUEIf maRules( InxIsName, rowId ) = enumDB_YES ThenDim myRegExp, FoundMatchSet myRegExp = New RegExpmyRegExp.Pattern = "^\s"If myRegExp.Test(fieldValue) ThenCall AddErrorMessage ( rowId, "Leading Space" )ValidValidForName = FALSEElsemyRegExp.Pattern = "\s$"If myRegExp.Test(fieldValue) ThenCall AddErrorMessage ( rowId, "Trailing Space" )ValidValidForName = FALSEEnd IfEnd IfEnd 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'-----------------------------------------------------------------------------------------------------------------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'-----------------------------------------------------------------------------------------------------------------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 = 11InxIsName = 12mNumOfRules = 13 ' 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%>