Solved

Search string for range of characters (validate event)

Posted on 2004-09-24
9
160 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 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