Federal09
Federal09

Reputation: 649

How to block or restrict special characters from textbox

I need exclude special characters (%,&,/,",' etc ) from textbox

Is it possible? Should I use key_press event?

string one = radTextBoxControl1.Text.Replace("/", "");
                string two = one.Replace("%", "");
                //more string
                radTextBoxControl1.Text = two;

in this mode is very very long =(

Upvotes: 11

Views: 115010

Answers (7)

StuKay
StuKay

Reputation: 317

Another way to exclude a varied selection of characters such as %,&,',A,b,2 would be to use the following in the TextBox KeyPress event handler:

e.Handled = "%&'Ab2".Contains(e.KeyChar.ToString());

To include a double quote to the exclusion list use:

e.Handled = ("%&'Ab2"+'"').Contains(e.KeyChar.ToString());

Note: This is case sensitive.

Upvotes: 0

Pooja
Pooja

Reputation: 1

we can validate it using regular expression validator

ValidationExpression="^[\sa-zA-Z0-9]*$"

<asp:TextBox runat="server" ID="txtname" />
        <asp:RegularExpressionValidator runat="server" ControlToValidate="txtname"
            ForeColor="Red" SetFocusOnError="true" Display="Dynamic"
            ErrorMessage=" Restrict for special characters" ID="rfvname"
            ValidationExpression="^[\sa-zA-Z0-9]*$">

        </asp:RegularExpressionValidator>

you can see also demo here https://www.neerajcodesolutions.com/2018/05/how-to-restrict-special-characters-in.html

Upvotes: 0

Bill Kindig
Bill Kindig

Reputation: 3622

The code below allows only numbers, letters, backspace and space.

I included VB.net because there was a tricky conversion I had to deal with.

C#

private void textBoxSample_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = e.KeyChar != (char)Keys.Back && !char.IsSeparator(e.KeyChar) && !char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar);
}

VB.net

Private Sub textBoxSample_KeyPress(sender As Object, e As KeyPressEventArgs) Handles textBoxSample.KeyPress
    e.Handled = e.KeyChar <> ChrW(Keys.Back) And Not Char.IsSeparator(e.KeyChar) And Not Char.IsLetter(e.KeyChar) And Not Char.IsDigit(e.KeyChar) 
End Sub

Upvotes: 7

Hisham
Hisham

Reputation: 1316

the best for me:

void textBoxSample_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = Char.IsPunctuation(e.KeyChar) ||  
                      Char.IsSeparator(e.KeyChar) || 
                      Char.IsSymbol(e.KeyChar);
    }

it will be more usefull to enable delete and backsapce Keys ...etc

Upvotes: 0

Francis Acosta
Francis Acosta

Reputation: 81

you can use this:

private void textBoxSample_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = !char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar);
    }

it blocks special characters and only accept int/numbers and characters

Upvotes: 7

Hossain Muctadir
Hossain Muctadir

Reputation: 3626

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;
    }
}

Upvotes: 21

Andrew Mack
Andrew Mack

Reputation: 312

You could use the 'Text Changed' event (I BELIEVE (but am not sure) that this gets fired on a copy/paste).

When the event is triggered call a method, let's say, PurgeTextOfEvilCharacters().

In this method have an array of the characters you want to "block". Go through each character of the .Text of the TextBox control and if the character is found in your array then you don't want it. Rebuild the string with the "okay" characters and you're good to go.

I'm betting there's a better way, but this seems okay to me!

Upvotes: 0

Related Questions