I need to validate a string, which might contain alphanumeric as well as special character, where as I have to pass the one which has only Alpha chars (no numbers or any other special characters are allowed)
In a current method I use ASCII numbers to evaluate each character if its alpha or not. Is there any other efficient way to discover the presence of special characters or numbers in the string? Like can’t we use Like or something to check once than going character by character?
For y = 2 To Len(sString)
If Not ((Asc(Mid$((sString,y,1))>64 AND Asc(Mid$((sString,y,1))<91) OR _
(Asc(Mid$((sString,y,1))>96 AND Asc(Mid$((sString,y,1))<123)) Then
//Display an error msg
Exit For
End If
Next y
You can use regular expressions in VB6. You have to add a reference to the “Microsoft VBScript Regular Expressions 5.5” library to your project. You can then use the following:
When I originally answered this question, it was tagged as VB.NET; for future reference, my original .Net-based answer is retained below
As you thought, this can be done with regular expressions (don’t forget
Imports System.Text.RegularExpressions:Also, the original code reads like VB6 code, not VB.NET. Here is a much more readable way to write the original non-regex code: