I have a textbox in my application, in this textbox, I want to perform a validation check. If user enter [, \\ .'] these characters, it should prompt message that invalid character. However, what I did is just the regex to check the characters, but it seems that C# have trouble recognize [\\'] these simple. Are there a ways to deal with these special characters?
I am assuming you are trying to keep only alpha-numeric and space characters. Add a keypress event like this
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
var regex = new Regex(@"[^a-zA-Z0-9\s]");
if (regex.IsMatch(e.KeyChar.ToString()))
{
e.Handled = true;
}
}
Try this code
private static readonly char[] SpecialChars = @"[,.']".ToCharArray();
int indexOf = YourInputString.IndexOfAny(SpecialChars);
if (indexOf != -1)
{
//Print Contains Invalid Character
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.