Solved

Search string for range of characters (validate event)

Posted on 2004-09-24
9
146 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 10

Expert Comment

by:fds_fatboy
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 50

Accepted Solution

by:
Dave Brett earned 125 total points
Comment Utility
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
Comment Utility
brettdj
I think we have a winner.
0
 
LVL 50

Expert Comment

by:Dave Brett
Comment Utility
thanks for the compliment :)
0
 

Author Comment

by:Victoria_
Comment Utility
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
Comment Utility
Thanks for the grade Victoria
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now