?
Solved

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

Posted on 2016-11-08
11
Medium Priority
?
65 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 52

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 29

Accepted Solution

by:
Pawan Kumar earned 2000 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 52

Expert Comment

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

 
LVL 29

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 29

Expert Comment

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

Regards,
Pawan
0
 
LVL 52

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 29

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 29

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 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