470,594 Members | 1,120 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

How do i restrict entering non-numeric data

sp
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
Nov 18 '05 #1
2 1552
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" <sp@discussions.microsoft.com> wrote in message
news:26**********************************@microsof t.com...
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

Nov 18 '05 #2
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

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by vijay21 | last post: by
28 posts views Thread by gc | last post: by
10 posts views Thread by sconeek | last post: by
3 posts views Thread by divaof_7 | last post: by
1 post views Thread by =?Utf-8?B?Qw==?= | last post: by
285 posts views Thread by Sheth Raxit | last post: by
reply views Thread by copx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.