How do i restrict entering non-numeric data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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!
***************************
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top