Solved

Search string for range of characters (validate event)

Posted on 2004-09-24
9
152 Views
Last Modified: 2010-05-18
Hi,

I have a textbox, when people paste someting in it, not being letters, I want a boolean set to true.
So characters like: é,@#$%^&*()678., etc are not allowed in the string in that case the string should be set to true. Do I have to make a loop or ????

Thank you in advance.


0
Comment
Question by:Victoria_
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12142290
A loop would be the best way.  When submitting the text box (validate, cmdbutton_click, whatever):

 - Create an array of characters that you will disallow
 - Loop through the array from LBound(array()) to Ubound(array())
 - Use InStr to check if the current character is in the text box
 - Send error if it is
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12142324
Either that or loop through all characters in the text box, checking if the ASCII value (using ASC()) is within a valid range?
   A=65, Z=90
   a=98, z=122

I can post code for either if you wish...
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12142457
Or use the like operator in a loop like this:

Private Function ValidateAlphaString (ByVal AlphaString As String) As Boolean
'Returns true if valid - all alphabetic characters
    Dim i as Long

    For i = 1 to Len(AlphaString)
        if not Mid$(AlphaString,i,1) Like "[A-Z][a-z]" Then
            ValidateAlphaString = False
            Exit Function
        End if
    Next
    ValidateAlphaString  = True
End Function

When you want to validate your string do something like this:
    blnInvalid = Not ValidateAlphaString(Text1.Text)

Also - if you want to stop users from typing in non alphas you could put use the KeyPress or KeyDown events of your textbox withthe caveate that it is dificult to stop your user pasting invalid text from the textbox.


0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12142512
Sorry - my coding mistake - try this code instead

Private Function ValidateAlphaString(ByVal AlphaString As String) As Boolean
'Returns true if valid - all alphabetic characters
    Dim i As Long

    For i = 1 To Len(AlphaString)
        If Not Mid$(AlphaString, i, 1) Like "[A-Za-z]" Then
            ValidateAlphaString = False
            Exit Function
        End If
    Next
    ValidateAlphaString = True
End Function
0
 
LVL 50

Accepted Solution

by:
Dave Brett earned 125 total points
ID: 12143461
If you use RegExp you don't need a loop

This function allows alphabetical characters and spaces

Private Function Alpha(ByVal AnyStr As String) As Boolean
    Dim RegEx
    Set RegEx = CreateObject("vbscript.regexp")
    With RegEx
        .Global = True
        .Pattern = "([a-z]|\s+)"
        .ignorecase = True
    End With
    If Len(RegEx.Replace(AnyStr, "")) = 0 Then Alpha = True
    Set RegEx = Nothing
End Function

Cheers

Dave
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12144003
brettdj
I think we have a winner.
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 12144068
thanks for the compliment :)
0
 

Author Comment

by:Victoria_
ID: 12153956
Thank you very much brettdj for the code. All the others thank you for the effort ( especially fds_fatboy).
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 12154047
Thanks for the grade Victoria
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question