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

  • Home
  • SEARCH
  • 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 6040209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:28:50+00:00 2026-05-23T06:28:50+00:00

How to validate a TextBox field against a Natural Number ? User are restricted

  • 0

How to validate a TextBox field against a Natural Number? User are restricted to input natural number (e.g. 1,2,3..99999) and if not, a MessageBox is shown.

Currently I’m using the following code (assuming the natural number does not go beyond two digits):

Regex isPositiveNum2 = new Regex("[1-9]");
Regex isPositiveNum = new Regex("[1-9][1-9]");
if (isPositiveNum.IsMatch(textbox1.Text) == true || 
    isPositiveNum2.IsMatch(textbox1.Text) == true)
{
    /* Do something */
}
else
{
    MessageBox.Show("Hey! This is not a Natural Number");
    textbox1.Text = "1";
}

This works alright, but I’m sure it’s not the best approach. Hope something can suggest something better.

  • 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-05-23T06:28:51+00:00Added an answer on May 23, 2026 at 6:28 am

    Use the built in parsing:

    int.Parse(textbox1.Text) > 0
    

    This will throw an exception if textbox1.Text can’t be parsed, so you may want to use TryParse instead:

    int test;
    if(int.TryParse(textbox1.Text, out test))
    {
      // parse succeeded can check if natural
      if(test > 0)
      {
        // do something
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to validate an xml file against its associated schema using Visual
I need to validate an XML string (and not a file) against a DTD
I'm trying to validate an ASP TextBox that can only have 1-2 digits (not
I want to validate the value a user enters in a text box, so
I need to validate a date/time field on a webpage but want it to
I am trying to validate user id's matching the example: smith.jack or smith.jack.s In
I'm using jQuery validate on a form I am building and it is working
I want to validate all the TextBox controls on the page load event. I'm
In a gridview I have the following Template Field containing a textbox <asp:TemplateField ShowHeader=true
Hi I want to validate a form using a button, but this button call

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.