Here is a class I have written which defines some basic validation routines:
' Copyright (c) Pipalia IT Consultants 2010' Please keep the above copyright line, if you decide to use this class in your codeClass ValidationErrorstrErrorMsg() As String ' Array to store messagescounter As Integer' Initiate the queueSub New()counter = 0End Sub' Add error to an arraySub AddError(strMsg As String)Redim Preserve strErrorMsg(counter)strErrorMsg(counter) = strMsgcounter = counter + 1End Sub' get all error messagesFunction GetError() As StringDim strError As StringForall msg In strErrorMsgIf strError = "" ThenstrError = msgElsestrError = strError + Chr(13) + msgEnd IfEnd ForallGetError = strErrorEnd Function' determine whether an error has been raisedFunction IsError() As BooleanIf counter = 0 ThenIsError = FalseElseIsError = TrueEnd IfEnd Function' check if field value is emptySub EmptyFieldError(strFieldVal As String, strMsg As String)If Trim(strFieldVal) = "" ThenAddError(strMsg)End IfEnd Sub' check if given value (converted to single data-type) is positiveSub PositiveValueCheck(strFieldVal As String, strMsg As String)Dim sFieldVal As SingleIf Trim(strFieldVal) <> "" ThenIf Isnumeric(strFieldVal) ThensFieldVal = Csng(strFieldVal)If sFieldVal <>AddError(strMsg)End IfElseAddError(strMsg)End IfEnd IfEnd SubSub IsGreaterThanZero(strFieldVal As String, strMsg As String)Dim sFieldVal As SingleIf Trim(strFieldVal) <> "" ThenIf Isnumeric(strFieldVal) ThensFieldVal = Csng(strFieldVal)If sFieldVal <= 0 ThenAddError(strMsg)End IfElseAddError(strMsg)End IfEnd IfEnd Sub' Check if given card number is a numeric valueSub IsCardNumber(strFieldVal As String, strMsg As String)Dim strCharacter As StringFor x = 1 To Len(strFieldVal)strCharacter = Mid(strFieldVal, x, 1)If (Asc(strCharacter) > 47 And Asc(strCharacter) <>' Numeric valueElseAddError(strMsg)Exit ForEnd IfNextEnd Sub' Raise an error if field value length is higher than maximumSub CheckValueLength(strFieldVal As String, intSize As Integer, strMsg As String)If Len(strFieldVal) > intSize ThenAddError(strMsg)End IfEnd Sub' Used to add any generic error message to the listSub GeneralError(strMsg As String)AddError(strMsg)End SubEnd Class
We will look at what this code does and how to use it during validation in the next session.