Friday, September 4, 2009

VB6 Function for AlphaNumeric Checking

The following are some useful VB6 validation checking functions.

Public Function IsAlphaBetical(TestString As String) As Boolean

Dim sTemp As String
Dim iLen As Integer
Dim iCtr As Integer
Dim sChar As String

'returns true if all characters in a string are alphabetical
'returns false otherwise or for empty string

sTemp = TestString
iLen = Len(sTemp)
If iLen > 0 Then
For iCtr = 1 To iLen
sChar = Mid(sTemp, iCtr, 1)
If Not sChar Like "[A-Za-z]" Then Exit Function
Next

IsAlphaBetical = True
End If

End Function

Public Function IsAlphaNumeric(TestString As String) As Boolean

Dim sTemp As String
Dim iLen As Integer
Dim iCtr As Integer
Dim sChar As String

'returns true if all characters in a string are alphabetical
' or numeric
'returns false otherwise or for empty string

sTemp = TestString
iLen = Len(sTemp)
If iLen > 0 Then
For iCtr = 1 To iLen
sChar = Mid(sTemp, iCtr, 1)
If Not sChar Like "[0-9A-Za-z]" Then Exit Function
Next

IsAlphaNumeric = True
End If

End Function

Public Function IsNumericOnly(TestString As String) As Boolean

Dim sTemp As String
Dim iLen As Integer
Dim iCtr As Integer
Dim sChar As String

'returns true if all characters in string are numeric
'returns false otherwise or for empty string

'this is different than VB's isNumeric
'isNumeric returns true for something like 90.09
'This function will return false

sTemp = TestString
iLen = Len(sTemp)
If iLen > 0 Then
For iCtr = 1 To iLen
sChar = Mid(sTemp, iCtr, 1)
If Not sChar Like "[0-9]" Then Exit Function
Next

IsNumericOnly = True
End If

End Function

2 comments:

Anonymous said...

its good

Anonymous said...

I modified the functions to get only one with two arguments :

Public Function fncIsAlphaNumeric(strStringToTest As String, bytTestStyle As Byte) As Boolean

' bytTestStyle = 0 for Alphanumeric, 1 for Numeric only, 2 for Alphabetical only
' Any other value will return False
' An empty string returns False

Dim strCharSet As String
Dim intI As Integer
Dim strCharacter As String

Select Case bytTestStyle
Case 0
strCharSet = "[0-9A-Za-z]"
Case 1
strCharSet = "[0-9]"
Case 2
strCharSet = "[A-Za-z]"
Case Else
Exit Function
End Select


If Len(strStringToTest) > 0 Then
For intI = 1 To Len(strStringToTest)
If Not Mid$(strStringToTest, intI, 1) Like strCharSet Then Exit Function
Next

fncIsAlphaNumeric = True
End If
End Function