how can i disallow spaces or any or even numbers in text box?

Hi evry1,

i have a question that how can i disallow spaces in the textbox or any character that i dont want the user to textit ,

thx

(thats my 1st time here ^_^)




Answer this question

how can i disallow spaces or any or even numbers in text box?

  • MSatya

    i dont recommend to do it that way... becasue if u use the keypress if the user paste text ur validation will not work.. what i use is textchanged... i get the string that is in the textbox and create a char array ,, then i validate char by char... heres an example. (if u use this often u can create a control...so u stop pasting in each class the method... but what ever... heres the code):

    //u create the event for ur textbox text changed.. and send to the method validateInt a textbox control. that u want to validate

    private void txtCantidad_TextChanged(object sender, EventArgs e)
    {
    validanumero(txtCantidad);
    }


    private void validateInt (TextBox sender)
    {
    //a flag to define if it should select the end of the text so it looks pretty :P
    bool selectEnd = false;
    // the string newText will be the final result
    string newText = null;
    //here its gets the text of sender.. sender is the textbox that u sent
    string txt = sender.Text;
    //creates an array of chars of ur textbox text
    char[] txtArray = txt.ToCharArray();
    //for each character it starts the validation
    foreach (char car in txtArray)
    {
    //i car is > 47 and < 58 the its a valid character and add to newtext the character... if u dont know what is 47 and 48 look in google for an ASCII table and each character contains a number... thats it
    if (car > 47 && car < 58)
    {
    newText = newText + car;
    }
    //heres what i said about selecend... if the character is not valid then it will select the last part of the text.. if u dont understand me,, just dont do it and check out what happen :)
    else
    {
    selectEnd = true;
    }
    }
    //ok,,now it ended and the text of the text is = to newText;
    sender.Text = newText;
    //now figures out what part of the text to select .. as i said if uj didnt get my point on here :P ,, remove it and check out ;)
    if (selectEnd == true)
    {
    int x = 0;
    try
    {
    x = newText.Length;
    }
    catch (Exception)
    {
    newText = "";
    }
    sender.SelectionStart = x;
    }
    }

    hope it helps... if u need to validate floats just change the characters number and there u go ;)

  • ToroLocoMex

    hi ,

    actually im learning c# now and im not good enough yet by enevts if i dont bother you can u write the code please so i guess i can understand how does it works .

    thx



  • tim_baxter

    Handle the KeyPressed event and if the key passed in through the event args is not one you want, then sent e.Handled = true;

    Be careful tho, if char.IsControl returns true, you want to leave e.Handled, otherrwise the arrows, etc will not work.



  • adimasi

    hi,

    you can try something like this


    public partial class Form1 : Form

    {
    public Form1()
    {
    InitializeComponent();
    //keypress event

    this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
    }
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {

    //first character in this string is space

    string rejectthose = " ><,-+{}[]";
    if (rejectthose.IndexOf(e.KeyChar) > -1)
    {
    MessageBox.Show("you can't use this character " + e.KeyChar);
    e.Handled =
    true;
    }
    }
    }


    hope this helps



  • how can i disallow spaces or any or even numbers in text box?