[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 73
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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