Click here to Skip to main content
15,798,066 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this is apply on web application i don't want to use javascript

What I have tried:

protected void TextBox1_TextChanged(object sender, EventArgs e)

string text = ((TextBox)sender).Text;
var regex = new Regex(@"[^a-zA-Z0-9\s]");
if (!regex.IsMatch(text.ToString()))
return false;
Updated 27-Apr-18 3:25am
F-ES Sitecore 27-Apr-18 8:17am    
You can't stop that using the server-side events as those events only fire after the user has updated the textbox and the form has been submitted. You could strip the unwanted text in that event if you wanted to, but you can't stop it. You'll need to use javascript to do that.

1 solution

Don't even try to do this server side - not only would it mean a round trip to the server every time the user types a key, which would really mess up typing speeds, it's a spectacularly bad way to think, even if you could do it, which you can't.

You need to do this in Javascript: How to restrict special characters and spaces in textbox via javascript?[^]
Share this answer
F-ES Sitecore 27-Apr-18 9:09am    
That event is only called (assuming autopostback) when the textbox loses focus, not on each keypress.

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