Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7839067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:18:54+00:00 2026-06-02T15:18:54+00:00

The problem is simple: I have a form with textboxes, and in one of

  • 0

The problem is simple: I have a form with textboxes, and in one of those textboxes, I want users to enter either a positive or negative 2-digit number. I’m looking for an easy way to enforce this restriction, i.e. without having to parse the number and check whether its absolute value is below 100.

If I set the textbox maxlength to 2, they cannot enter numbers below -9. If the maxlength is 3, they can enter numbers above 99.

A masked textbox has no solution, I cannot make the “-” literal optional, or at least not that I know of.

What would be the simplest solution to this restriction: “An empty textbox will accept 2 input characters if no “-” is typed, otherwise it accepts 3″? Handling the change event to see if a “-” was typed and resetting maxlength seems a bit overkill…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:18:58+00:00Added an answer on June 2, 2026 at 3:18 pm

    You actually want a NumericUpDownControl. Use that, and set its Minimum and Maximum properties. No validation code required at all, and easily solves your problem.

    If you’re stuck on using a TextBox then in short you’re not going to get the desired functionality you want without actually checking the number. You could write a UserControl to do this and you could reuse it, but you still have to write some validation code.

    Just validate the integer using ASP.NET/Javascript style validation. This example uses a label with a ForeColor of Red to display validation messages.

    private void numberTextBox_TextChanged(object sender, EventArgs e) {
        int number;
    
        bool isValid = int.TryParse(numberTextBox.Text, out number);
    
        if (!isValid) {
            validationLabel.Text = "Must be a two-digit number.";
            validationLabel.Visible = true;
            return;
        }
    
        if (number < -99 || number > 99) {
            validationLabel.Text = "Must be between -99 and 99";
            validationLabel.Visible = true;
            return;
        }
    
        if (isValid) {
            validationLabel.Visible = false;
            // Do something else with your number
            // if you need to.
        }
    }
    

    There’s nothing wrong with having to write code to do validation. You probably won’t achieve good results using existing designer properties otherwise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the problem: I have a simple form (textboxes, etc). The bindings are
My problem is simple: I have a long list of elements that I want
I've written this fiddle to demo the problem. I have a simple form. When
I have a simple form with one text box that pass data to SQL
Problem 1: I have a simple winforms app and I want to DataBind my
I have a simple problem that i cannot solve. I have a dictionary: aa
My problem is quite simple: I have a 400MB file filled with 10,000,000 lines
I have a simple problem but I am not sure how to solve it.
I have a simple problem but no matter what I try I can't see
so I have a simple problem. I have a hover background image change effect,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.