|
-
Dec 24th, 2014, 05:23 PM
#1
Thread Starter
PowerPoster
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
-
Dec 24th, 2014, 05:42 PM
#2
Thread Starter
PowerPoster
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]+$"
-
Dec 24th, 2014, 05:43 PM
#3
Re: RegEx: Allow only A-Z, a-z and spaces.
[a-zA-Z ]
is the correct regex expression
-
Dec 24th, 2014, 05:47 PM
#4
Thread Starter
PowerPoster
Re: RegEx: Allow only A-Z, a-z and spaces.
 Originally Posted by Toph
[a-zA-Z ]
is the correct regex expression
I tried that also and the ErrorProvider still fires.
-
Dec 24th, 2014, 06:03 PM
#5
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)
-
Dec 24th, 2014, 06:10 PM
#6
Thread Starter
PowerPoster
Re: RegEx: Allow only A-Z, a-z and spaces.
 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.
-
Dec 24th, 2014, 06:14 PM
#7
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
 
-
Dec 24th, 2014, 06:14 PM
#8
Thread Starter
PowerPoster
Re: RegEx: Allow only A-Z, a-z and spaces.
 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.
-
Dec 24th, 2014, 06:16 PM
#9
Thread Starter
PowerPoster
Re: RegEx: Allow only A-Z, a-z and spaces.
 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.
-
Dec 24th, 2014, 06:17 PM
#10
Re: RegEx: Allow only A-Z, a-z and spaces.
Sorry, check my edit. That has the correct solution.
My usual boring signature: Nothing
 
-
Dec 24th, 2014, 06:21 PM
#11
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.
-
Dec 24th, 2014, 06:29 PM
#12
Thread Starter
PowerPoster
Re: RegEx: Allow only A-Z, a-z and spaces.
 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?
-
Dec 24th, 2014, 06:31 PM
#13
Re: RegEx: Allow only A-Z, a-z and spaces.
Look at my post, that example works fine. I changed up the Regx pattrn
-
Dec 24th, 2014, 06:36 PM
#14
Thread Starter
PowerPoster
Re: RegEx: Allow only A-Z, a-z and spaces.
 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.
-
Dec 24th, 2014, 07:29 PM
#15
Re: RegEx: Allow only A-Z, a-z and spaces.
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|