How do i restrict entering non-numeric data

Discussion in 'ASP .Net' started by =?Utf-8?B?c3A=?=, Oct 27, 2004.

  1. Hello Everybody,

    How can I restrict the user entering non-numeric data in textbox control
    on server side using asp.net. Any Code sample or suggestions are appreciated.

    Thanks in advance
    sp
    =?Utf-8?B?c3A=?=, Oct 27, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?c3A=?=

    Kikoz Guest

    Hey.

    Server side:

    protected TextBox txt;
    protected Label lbl;

    private void Page_Load(object sender,System.EventArgs e)
    {
    try
    {
    IsPostBack ? lbl.Visible = false : Verify();
    }
    catch(TextBoxIsNotValid)
    {
    lbl.Visible = true;
    lbl.Text = "Whatever message";
    }
    }
    private void Verify()
    {
    try
    {
    int.Parse(txt.Text.Trim());
    }
    catch
    {
    throw new TextBoxIsNotValid();
    }
    }

    Separate class somewhere:

    public class TextBoxIsNotValid : System.Exception
    {
    public TextBoxIsNotValid() : base("Some message to log if needed.");
    }

    Hope it'll help.
    Kikoz

    "sp" <> wrote in message
    news:...
    > Hello Everybody,
    >
    > How can I restrict the user entering non-numeric data in textbox control
    > on server side using asp.net. Any Code sample or suggestions are
    > appreciated.
    >
    > Thanks in advance
    > sp
    Kikoz, Oct 27, 2004
    #2
    1. Advertising

  3. With JavaScript, you can capture keystrokes, if you wish to go that far. On
    the server side, you can validate and refuse bad data. It is your choice how
    heavy you want your front end to be.


    ---

    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    ***************************
    Think Outside the Box!
    ***************************

    "sp" wrote:

    > Hello Everybody,
    >
    > How can I restrict the user entering non-numeric data in textbox control
    > on server side using asp.net. Any Code sample or suggestions are appreciated.
    >
    > Thanks in advance
    > sp
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN, Oct 27, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    370
  2. Replies:
    5
    Views:
    915
    X-Centric
    Jun 30, 2005
  3. =?Utf-8?B?Qw==?=

    restrict the user entering nonalphanumeric keys?

    =?Utf-8?B?Qw==?=, Jun 13, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    562
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
    Jun 13, 2007
  4. darrel
    Replies:
    4
    Views:
    795
    darrel
    Jul 19, 2007
  5. Colin Howell
    Replies:
    2
    Views:
    560
    John Bokma
    Apr 25, 2010
Loading...

Share This Page