Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Search string for range of characters (validate event)

Posted on 2004-09-24
9
Medium Priority
?
161 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 500 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 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