Solved

Search string for range of characters (validate event)

Posted on 2004-09-24
9
154 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Advice in Xamarin 21 97
VBA - Excel, Hide/unhide range of rows on sheet with listbox selection 9 64
backup program with robocopy 6 47
Collapse and expand table in Word 2010 2 39
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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

821 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