Solved

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

Posted on 2016-11-08
11
61 Views
Last Modified: 2016-11-09
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
Comment
Question by:Member_2_7967119
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41878256
Hi,

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

Open in new window


Regards
0
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41878331
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
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41878338
I wouldn' t use
@"^[0-9]*$"

since it matches empty strings
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41878340
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
 

Author Comment

by:Member_2_7967119
ID: 41880462
^\d+$ fails since it allows empty space. I want only whole numbers without decimals to go through. Thanks.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41880463
Hi Author,
Have you tried mine code?

Regards,
Pawan
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41880478
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
 

Author Comment

by:Member_2_7967119
ID: 41881683
Pawan,

^[0-9]*$ does work. Thank you
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41881687
Can you provide us few sample strings you are using ?
0
 

Author Closing Comment

by:Member_2_7967119
ID: 41881688
Thank you for your help.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41881689
ohh !! Great ..
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question