Results 1 to 15 of 15

Thread: RegEx: Allow only A-Z, a-z and spaces.

Hybrid View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    RegEx: Allow only A-Z, a-z and spaces.

    Hi, All,

    I am trying to validate a textbox. I only want to allow spaces and alpha characters. Numbers and special characters not permitted.
    Regardless of what I type into the textbox, the ErrorProvider is firing and not sure why. What am I doing wrong? I am not very familiar with RegEx.

    Code:
        Private Sub OptionToAdd_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles OptionToAdd.Validated
            ErrorProv.SetError(OptionToAdd, String.Empty)
        End Sub
    
        Private Sub OptionToAdd_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OptionToAdd.Validating
            If (Regex.IsMatch("^[a-zA-Z ]+$", OptionToAdd.Text.ToString.Trim)) Then
                ErrorProv.SetError(OptionToAdd, String.Empty)
                OptionAdd.Enabled = True
                ResetForm.Enabled = True
                e.Cancel = False
            Else
                ErrorProv.SetError(OptionToAdd, "Only letters and white space are permitted. Please rectify and try again.")
                OptionToAdd.SelectAll()
                OptionAdd.Enabled = False
                ResetForm.Enabled = False
                e.Cancel = True
            End If
        End Sub

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: RegEx: Allow only A-Z, a-z and spaces.

    I have even tried the following and the ErrorProvider still fires. I am lost with RegEx.

    "^[a-zA-Z\s]+$"

  3. #3
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: RegEx: Allow only A-Z, a-z and spaces.

    [a-zA-Z ]

    is the correct regex expression

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: RegEx: Allow only A-Z, a-z and spaces.

    Quote Originally Posted by Toph View Post
    [a-zA-Z ]

    is the correct regex expression
    I tried that also and the ErrorProvider still fires.

  5. #5
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: RegEx: Allow only A-Z, a-z and spaces.

    Subscribe to the TextChanged of the TextBox event. And in that sub put.

    TextBox1.Text = Regex.Replace(TextBox1.Text, "[a-zA-Z ]", String.Empty)

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: RegEx: Allow only A-Z, a-z and spaces.

    Quote Originally Posted by Toph View Post
    Subscribe to the TextChanged of the TextBox event. And in that sub put.

    TextBox1.Text = Regex.Replace(TextBox1.Text, "[a-zA-Z ]", String.Empty)
    If I use that and start typing, nothing happens at all.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: RegEx: Allow only A-Z, a-z and spaces.

    Quote Originally Posted by Toph View Post
    Subscribe to the TextChanged of the TextBox event. And in that sub put.

    TextBox1.Text = Regex.Replace(TextBox1.Text, "[a-zA-Z ]", String.Empty)
    I put that in the LostFocus event and I typed in:

    Hello World!

    When the textbox lost focus, all that was left was the special character.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: RegEx: Allow only A-Z, a-z and spaces.

    What is the string that you are typing into that? I tried the supplied RegEx in a tester and it certainly appears to act as I would expect. Of course, I tested with valid and invalid strings, but I don't know what you are using, or how you are testing. Are you typing into the textbox? are you copying and pasting? Is it being filled by code?

    EDIT: However, I should note that there is an even simpler solution. Take a look at this link:

    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    The arguments to the IsMatch are input followed by pattern, which is the opposite of how you supplied them. In other words, you have the arguments backwards.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: RegEx: Allow only A-Z, a-z and spaces.

    Quote Originally Posted by Shaggy Hiker View Post
    What is the string that you are typing into that? I tried the supplied RegEx in a tester and it certainly appears to act as I would expect. Of course, I tested with valid and invalid strings, but I don't know what you are using, or how you are testing. Are you typing into the textbox? are you copying and pasting? Is it being filled by code?
    textbox. I am typing in the characters. Tried lostfocus, textchanged, validated/validating events, etc.
    regardless of what I do, the errorprovider still fires. I even cleaned the solution and tried it again. no change for me.
    all i wish to allow for input is spaces and alpha characters only.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: RegEx: Allow only A-Z, a-z and spaces.

    Sorry, check my edit. That has the correct solution.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: RegEx: Allow only A-Z, a-z and spaces.

    Quote Originally Posted by Shaggy Hiker View Post
    Sorry, check my edit. That has the correct solution.
    Simple stuff I overlooked. Only issue is, even though the RegEx pattern states [a-zA-z ] it is allowing the special characters. maybe my pattern is wrong?

  12. #12
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: RegEx: Allow only A-Z, a-z and spaces.

    Sorry I freestyled that code.
    Here
    Code:
            TextBox1.Text = Regex.Replace(TextBox1.Text, "[^a-zA-Z ]", String.Empty)
            TextBox1.Select(TextBox1.Text.Length, 0)
    Works perfectly. Enjoy.


    Put that in the TextChanged event handler.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: RegEx: Allow only A-Z, a-z and spaces.

    Quote Originally Posted by Toph View Post
    Sorry I freestyled that code.
    Here
    Code:
            TextBox1.Text = Regex.Replace(TextBox1.Text, "[^a-zA-Z ]", String.Empty)
            TextBox1.Select(TextBox1.Text.Length, 0)
    Works perfectly. Enjoy.


    Put that in the TextChanged event handler.
    Thanks for the code. Work pretty well and looks like I can prevent dumb end users from inputting invalid info using this method.

  14. #14
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: RegEx: Allow only A-Z, a-z and spaces.

    Quote Originally Posted by Toph View Post
    Sorry I freestyled that code.
    Here
    Code:
            TextBox1.Text = Regex.Replace(TextBox1.Text, "[^a-zA-Z ]", String.Empty)
            TextBox1.Select(TextBox1.Text.Length, 0)
    Works perfectly. Enjoy.


    Put that in the TextChanged event handler.
    There is no need for the upper case A-Z because of the i flag in the end, which matches in a case-insensitive manner
    My Github - 1d3nt

  15. #15
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: RegEx: Allow only A-Z, a-z and spaces.

    Look at my post, that example works fine. I changed up the Regx pattrn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width