Hello Everyone:
Can any one please provide me with some sample code on how to validate that a variable contains only letters and numbers? I had been trying with the LIKE operator but something is not getting through me as I keep changing things and the value is always incorrect.
I would like the value “Vendor 4 North” to be valid and “Vendor$ #4 North” to be invalid. I don’t want the user to be able to enter any special characters only letters upper or lower case and numbers.
I know this may be so simple but it is driving me crazy.
Thanks
Subject: Use a function to do a char compare
I don’t have a sample of code at my fingertips but how about a function that returns a boolean and you pass the function a char and in the function there is a string of allowable characters - “ABCDEFabcdef123456” etc. You loop through the string you are checking 1 char at a time and and then an instr$ would validate quickly. If it fails you return false. In your calling loop you just have an if not true check then error and end…otherwise it just keeps going through all the characters in your string.
Probably not the best way but was the first thing that came to mind.
Steven
Subject: You could use RegExp
Dim regExp as VariantSet regExp = CreateObject(“VbScript.RegExp”)
and then use the appropriate pattern (see here for more details and examples including the one you want => http://www.15seconds.com/issue/010301.htm)
Subject: Function to check alphanumerics
This should work for alpha numerics …
Function isAlphanumeric(teststring As String) As Boolean
Dim rtn As Boolean
Dim i As Long
Dim lett As string
rtn = true
For i = 1 To Len(teststring)
lett = Mid(teststring,i,1)
' Asc(0) = 48, Asc(9) = 57 , Asc(A) = 65, Asc(z) = 122
If (Asc(lett) < 65 Or Asc(lett) > 122) And (Asc(lett) < 48 Or Asc(lett) > 57) Then
rtn = false
End If
If rtn = False Then i = Len(teststring) + 1
Next
isAlphanumeric = rtn
End Function
You will need to check the sac for spaces and any other white space characters you want to include