CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2010
    Posts
    15

    How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...)

    Hi guys,
    I just wonder how i can validate a TextBox( like txtName) to accept just string value and not any numeric value?
    Cloud u please let me know how I can do it? should I use a try and catch block?
    Thanks

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...

    No, for the love of God, don't use a try/catch block to control the flow of your program

    This is easily done one of two ways.

    1. Use a MaskedTextBox if you need a specific format.
    2. Handle the KeyDown event and check the input. If it is numeric, set e.Handled to true and nothing will appear in the TextBox.
    Last edited by BigEd781; June 20th, 2010 at 10:21 PM.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...

    In this case you would want to set e.handled to true if it is numeric since the goal is to not allow numeric characters. If you are going for names then you may also want to trap most other characters and allow only A-z as well as backspace.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...

    yes, data is right. I must have read the first post backward.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  5. #5
    Join Date
    Jun 2006
    Posts
    148

    Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...

    You may use the following code to validate,
    Code:
    //Keypress event of your textbox
    private void txtTarget_KeyPress(object sender, KeyPressEventArgs e)
            {
                int x = (int)e.KeyChar;
                e.Handled = !ValidateAlpha(x);
            }
    
    //Validation function
    public static bool ValidateAlpha(int keyAscii)
            {
                if (keyAscii >= 65 && keyAscii <= 90)
                    return true;
                else if (keyAscii >= 97 && keyAscii <= 122)
                    return true;
                else if (keyAscii == 8 || keyAscii == 13)
                    return true;
                return false;
            }

  6. #6
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...

    There is a .net regular expression validator control to use in asp.net.

  7. #7
    Join Date
    Jul 2010
    Posts
    82

    Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...

    Quote Originally Posted by Alsvha View Post
    There is a .net regular expression validator control to use in asp.net.
    Iam sure the code above would have helped you in ASP.net or C#. Why dont you reply or atleast mark it as resolved?

    Regards,
    CT.

    Check out www.cuteassistant.com - Manage your tasks, take notes, securely store data and more!

  8. #8
    Join Date
    Jul 2010
    Posts
    82

    Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...

    KeyDOwn event should work. You can check for an ascii range, else set e.cancel = true, so the key will not be processed. It works well.

    www.cuteassistant.com - Increase your productivity! Be more successful in life!

  9. #9
    Join Date
    Jul 2010
    Posts
    8

    Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...

    A simple textbox to handle this.

    public class TextOnlyBox:TextBox
    {

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
    //Mark the event as handled, the base method won't process this.
    e.Handled = (char.IsNumber(e.KeyChar));
    base.OnKeyPress(e);
    }

    protected override void OnValidating(CancelEventArgs e)
    {
    //Validation is required, as someone can copy paste the data
    //I even override the Text property to format the values
    //Remove any number, if there
    char[] CharsInTheText = this.Text.ToCharArray();
    //use a stringbuilder, it is very efficient for appeding string
    StringBuilder StringBuilder = new StringBuilder(CharsInTheText.Length, CharsInTheText.Length);

    foreach (char CharInTheText in CharsInTheText)
    {
    if (! char.IsNumber(CharInTheText))
    {
    StringBuilder.Append(CharInTheText);
    }

    }
    Text = StringBuilder.ToString();

    base.OnValidating(e);
    }

    }
    Last edited by partho; July 15th, 2010 at 12:28 AM. Reason: found a bug in my code sample, corrected it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured