• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 76
  • Last Modified:

RegularExpression for Numbers with no decimal points in ASP.net text control

What is the ValidationExpression to be used for a ASP.net text box  control to ensure only whole numbers are accepted (0 to 9) without any decimals?

I am using the following ValidationExpression

"^[0-9]+$"
0
Member_2_7967119
Asked:
Member_2_7967119
  • 5
  • 3
  • 3
1 Solution
 
Rgonzo1971Commented:
Hi,

I think it's fine, do you want thousand separators
Shorter
"^\d+$"

Open in new window


Regards
0
 
Pawan KumarDatabase ExpertCommented:
You need below if (  Regex.IsMatch("12345", @"^[0-9]*$" ))

 if (  Regex.IsMatch("12345", @"^[0-9]*$" ))
                
                 Console.WriteLine("match - [0-9]");
            
 else            
                
                Console.WriteLine("Other");

Open in new window


Hope it helps!!
0
 
Rgonzo1971Commented:
I wouldn' t use
@"^[0-9]*$"

since it matches empty strings
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Pawan KumarDatabase ExpertCommented:
Thank you Rgonzo1971. I think user will not be allowed to click on the submit button in that case. We can also use below if thats the issue.

            string str = "";
            if ( str.Length > 0 && Regex.IsMatch(str, @"^[0-9]*$" ))
                
                 Console.WriteLine("match - [0-9]");
            
            else            
                
                Console.WriteLine("Other");

Open in new window

0
 
Member_2_7967119Author Commented:
^\d+$ fails since it allows empty space. I want only whole numbers without decimals to go through. Thanks.
0
 
Pawan KumarDatabase ExpertCommented:
Hi Author,
Have you tried mine code?

Regards,
Pawan
0
 
Rgonzo1971Commented:
Cannot replicate your error
        string str = "";
        if ( Regex.IsMatch(str, @"^\d+$"))

            Console.WriteLine("match - [0-9]");

        else

            Console.WriteLine("Other");

Open in new window

0
 
Member_2_7967119Author Commented:
Pawan,

^[0-9]*$ does work. Thank you
0
 
Pawan KumarDatabase ExpertCommented:
Can you provide us few sample strings you are using ?
0
 
Member_2_7967119Author Commented:
Thank you for your help.
0
 
Pawan KumarDatabase ExpertCommented:
ohh !! Great ..
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now