Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to validate textbox which allows alphanumaric and underscore "_" by using regularexpressions in following method in WPF.
But it allows all the values. I need any modifications in this code or any alternative method that is available...Please look into this.
Thanks.
C#
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
    if (alphaNumericCheck(e.ToString()))
    {
        e.Handled = true;
    }
    else
        e.Handled = false;
}

private bool alphaNumericCheck(string p)
{
    if (regex.IsMatch(p))
        return true;
    else
        return false;
}
Posted
Updated 28-May-10 3:08am
v2
Comments
Gregory Gadow 28-May-10 9:28am    
I expect that the problem is in the regular expression; it would be helpful to have that.

To allow alphanumeric and underscore change your regular expression to [^a-zA-Z_0-9]

Like so:

string expression = "[^a-zA-Z_0-9]";
string sIn = "SomeText 900_009";

sIn = Regex.Replace(sIn, expression, "");  


Also, using System.Text.RegularExpressions; needs to be included to use Regex.

The third parameter in the Replace method is the string you wish to replace any characters that don't match your regular expression.

The output of the above code would be SomeText900_009
 
Share this answer
 
v2
Comments
Anıl Yıldız 28-May-10 10:45am    
Oh, apperantly i don't know what alphanumeric means! Thanks, i added numbers to my answer as well.
William Winner 28-May-10 11:05am    
sorry, didn't see you used replace. changed my vote...
GregWyatt 28-May-10 11:17am    
Actually it tests all of the characters in the string and replaces any that don't fit the expression with the third parameter. When using Regex.Replace(string input, string expression, string replacewith)
GregWyatt 28-May-10 11:23am    
cool Thanks William Winner!
sankar.cs wrote:
if (alphaNumericCheck(e.ToString()))


Surely e.ToString() will be "System.Windows.Controls.TextChangedEventArgs"

Alan.
 
Share this answer
 
Hello, like the comment above stated, it should be a problem with your Regex which you did not provide. Give this a try and let me know.
C#
private bool alphaNumericCheck(string p)
{
    return new Regex("^[A-Za-z_0-9]").IsMatch(p);
}


Edit: Apperantly forgot about numbers + minor changes.

Thank you,
Anıl Yıldız.
 
Share this answer
 
v5
Comments
William Winner 28-May-10 11:04am    
Reason for my vote of 3
close but you're only actually checking the first character with that pattern
the other two are close, but they didn't actually check their regular expression. One of the other answers is close.

What they one of them gave you will only check the first character.

The pattern that you really need is

C#
return new RegEx("^[a-zA-z_0-9]*$");


The caret (^) says check starting at the beginning.

The brackets indicate what is available as a character.

The asterisk (*) says check for 0 or more values.

The $ says that they also have to be at the end as well.

This will ensure that the whole string matches those character choices, not just the first character.


And as one of the others said, you can't just pass in e.ToString(). That's never going to give you what you expect.

Really, from what you're doing, it looks like you may want to handle the keypress event instead.

Using the KeyPress, you could check the e.KeyChar against the regular expression and then decide whether it was handled or not.
 
Share this answer
 
v2
Comments
GregWyatt 28-May-10 11:13am    
Actually it tests all of the characters in the string and replaces any that don't fit the expression with the third parameter. When using Regex.Replace(string input, string expression, string replacewith)
Anıl Yıldız 28-May-10 19:20pm    
That was what i wrote the first time. Then i changed it, but yeah you were right.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900