Results 1 to 7 of 7

Thread: [RESOLVED] Fast Letters Only Validation

  1. #1

    Thread Starter
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Resolved [RESOLVED] Fast Letters Only Validation

    Hi,

    i am wanting to detect whether a short string has numeral or operation characters within it, so that i may reject this string, 1000s of these will be sifted through, therefore i am wanting this to be as fast as possible.

    Oviously checking the ascii codes of each character will not be efficient.
    Using multiple Instr or Replace functions would not be efficient either, would it?

    Please note, a string may contain letters and numbers, but is still rejected because of the numbers.

    What do you recommend?

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Fast Letters Only Validation

    The only way I know of to do it is to check each letter in the string... if they are short strings then the efficiency of it shouldn't be a huge issue..

    Now if you only want letters a to z... and anything else would be unacceptable then you can do this..
    VB Code:
    1. Function AllLetters(tString As String) As Boolean
    2.      Dim strLetters As String
    3.      Dim i As Integer
    4.      strLetters = "abcdefghijklmnopqrstuvwxyz"
    5.  
    6.      For i = 1 To Len(tString)
    7.           If Instr(strLetters, LCase(Mid(tString, i, 1))) = 0 Then
    8.                 AllLetters = False
    9.                 Exit Function
    10.           End If
    11.      Next
    12.  
    13.      AllLetters = True
    14. End Function

    If you know for sure it's only going to be numbers that make it unacceptable you can do it slightly different.
    VB Code:
    1. Function AllLetters(tString As String) As Boolean
    2.      Dim i As Integer
    3.  
    4.      For i = 1 To Len(tString)
    5.           If IsNumeric(Mid(tString, i, 1)) Then
    6.                   AllLetters = False
    7.                   Exit Function
    8.           End If
    9.      Next
    10.  
    11.      AllLetters = True
    12. End Function

  3. #3

    Thread Starter
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Fast Letters Only Validation

    steven,

    thanks alot for the idea's and samples.

    although still requiring comparison for each char, your first sample (where i wouldve used ascii) allows for an easy method to also allow - and ' without a seperate comparison to the alphabet, unlike the way i wouldve done it with ascii.

    seems like the best way to do it so far doesn't it, and peformance wise, i tested it out, and no noticable lag with it.

    I only made one slight change, which was to scrap lcase and mid
    lcase because i forgot to specify this has already been performed, and mid to the .net method .substring

    VB Code:
    1. Function AllLetters(ByVal tString As String) As Boolean
    2.         Dim strLetters As String
    3.         Dim i As Integer
    4.         strLetters = "abcdefghijklmnopqrstuvwxyz-'"
    5.  
    6.         Try
    7.             For i = 0 To Len(tString) - 1
    8.                 If InStr(strLetters, tString.Substring(i, 1)) = 0 Then
    9.                     AllLetters = False
    10.                     Exit Function
    11.                 End If
    12.             Next
    13.         Catch
    14.             MsgBox(Err.Description)
    15.         End Try
    16.  
    17.         AllLetters = True
    18.     End Function

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Fast Letters Only Validation

    I have a couple of points for you Phill. Normally you wouldn't notice a difference but if you are calling this function many times, it is actaully more efficient to use a Return statement than it is to use an Exit Function statement or allow execution to fall to the End Function statement.

    Also, you might like to test these variations to see if there is any appreciable difference in execution time:
    VB Code:
    1. Function IsValidString(ByVal text As String) As Boolean
    2.         If text Is Nothing Then
    3.             Return True
    4.         End If
    5.  
    6.         Dim validChars As Char() = "abcdefghijklmnopqrstuvwxyz-'".ToCharArray()
    7.  
    8.         For i As Integer = 0 To text.Length - 1
    9.             If Array.IndexOf(validChars, text.Chars(i)) = -1 Then
    10.                 Return False
    11.             End If
    12.         Next i
    13.  
    14.         Return True
    15.     End Function
    VB Code:
    1. Function IsValidString(ByVal text As String) As Boolean
    2.         If text Is Nothing Then
    3.             Return True
    4.         End If
    5.  
    6.         Dim validChars As Char() = "abcdefghijklmnopqrstuvwxyz-'".ToCharArray()
    7.         Dim textChars As Char() = text.ToCharArray()
    8.  
    9.         For Each ch As Char In textChars
    10.             If Array.IndexOf(validChars, ch) = -1 Then
    11.                 Return False
    12.             End If
    13.         Next ch
    14.  
    15.         Return True
    16.     End Function
    I'm quite sure that you could use regular expressions to accomplish the same task, but I don't know whether it would be faster or not. I don't know regular expressions well enough to give you the correct syntax but you may like to lok into it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: [RESOLVED] Fast Letters Only Validation

    Thanks Jm,

    i have used Return now, as for char arrays, i believe the only advantage is memory here, when i am heavy into this project again i will test actual times and see if this is the case or not.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Fast Letters Only Validation

    The advantage of char arrays is actually the fact that you don't have to repeatedly call methods or properties, which take longer than array access via an index. Whether that difference is significant is the question.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [RESOLVED] Fast Letters Only Validation

    Using jmcilhinney's idea of using regex:
    VB Code:
    1. Dim str As String = System.Text.RegularExpressions.Regex.Match(sInputstring, "[\w\-']+").ToString
    2.         If str.Length <> sInputstring.Length Then Return False

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width