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
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]+$"
Re: RegEx: Allow only A-Z, a-z and spaces.
[a-zA-Z ]
is the correct regex expression
Re: RegEx: Allow only A-Z, a-z and spaces.
Quote:
Originally Posted by
Toph
[a-zA-Z ]
is the correct regex expression
I tried that also and the ErrorProvider still fires.
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)
Re: RegEx: Allow only A-Z, a-z and spaces.
Quote:
Originally Posted by
Toph
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.
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.
Re: RegEx: Allow only A-Z, a-z and spaces.
Quote:
Originally Posted by
Toph
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.
Re: RegEx: Allow only A-Z, a-z and spaces.
Quote:
Originally Posted by
Shaggy Hiker
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.
Re: RegEx: Allow only A-Z, a-z and spaces.
Sorry, check my edit. That has the correct solution.
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.
Re: RegEx: Allow only A-Z, a-z and spaces.
Quote:
Originally Posted by
Shaggy Hiker
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?
Re: RegEx: Allow only A-Z, a-z and spaces.
Look at my post, that example works fine. I changed up the Regx pattrn
Re: RegEx: Allow only A-Z, a-z and spaces.
Quote:
Originally Posted by
Toph
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.
Re: RegEx: Allow only A-Z, a-z and spaces.
Quote:
Originally Posted by
Toph
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