Solved

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

Posted on 2016-11-08
11
58 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
  • 5
  • 3
  • 3
11 Comments
 
LVL 49

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 49

Expert Comment

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

since it matches empty strings
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 49

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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