Question

I've been going through solutions from here and dotnetperls to try and find a way to format my textbox to only allow numeric characters. I'm either just not getting it, or really don't understand the code to do what I want.

I have a form that changes depending on which button was pressed in another form, and I need the Age box to only allow numerical characters up to 99 (So the user can enter an age in the range of 1-99. I've looked into Regex methods and masked text boxes but just don't get it.

Would someone be able to point me in the direction of what I would use to achieve this?

My code reads as:

private void AddItem_Load(object sender, EventArgs e)
    {
        if (formName == "customer")
        {
            this.Text = "Add Customer";
            lblZero.Text = "ID:";
            lblOne.Text = "Customer Name:";
            lblTwo.Text = "Address Line 1:";
            lblThree.Text = "Address Line 2:";
            lblFour.Text = "Town:";
            lblFive.Text = "Postcode:";
            lblSix.Text = "Age:";

            txtID.Text = Convert.ToString(customerList.NewID());
            txtID.Enabled = false;

        }
        else
        {
            this.Text = "Add Product";
            lblZero.Text = "Product ID:";
            lblOne.Text = "Product Code:";
            lblTwo.Text = "Product Name:";
            lblThree.Text = "Product Description:";
            lblFour.Text = "Price:";
            lblFive.Text = " --------- ";
            lblSix.Text = " ---------";

            txtID.Text = Convert.ToString(productList.NewID());
            txtID.Enabled = false;
            txtFive.Enabled = false;
            txtSix.Enabled = false;

        }
    }

where the textboxes on the form are labelled txtZero.txt ~ txtSix.txt.

All help is much appreciated :)

Was it helpful?

Solution

I'd recommend just using NumericUpDown controls.

You can only enter numbers into them, and you can set a minimum and maximum allowed value.

numericUpDown1.Minimum = 1;
numericUpDown1.Maximum = 99;

OTHER TIPS

You can add a this:

    public static void NumericEvent(ref string text, ref KeyPressEventArgs e)
    {
        if (char.IsNumber(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Delete)
        {
            e.Handled = false; //ok
        }
        else
        {
            e.Handled = true; //not ok
        }
    }

And call with this:

    private void GenericNumeric_KeyPress(object sender, KeyPressEventArgs e)
    {
        TextBox tb = sender as TextBox;
        string field = tb.Text;
        NumericEvent(ref field, ref e);
        tb.Text = field;
    }

Think this one has been discussed a few times before...

How to allow only integers in a textbox?

If you literally want the user to be unable to enter non-numeric characters you'll need to do that in Javascript. Validators are just that, they validate the input following post back of the form. You simply add the validator and in your post back method e.g.

protect void btnSave_Click(object sender, EventArgs e)
{
    Page.Validate(); // validation message from the validator gets displayed 

    if (!Page.IsValid)
        return;  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top