Solved

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

Posted on 2016-11-08
11
45 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 48

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 18

Accepted Solution

by:
Pawan Kumar Khowal 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 48

Expert Comment

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

since it matches empty strings
0
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 18

Expert Comment

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

Regards,
Pawan
0
 
LVL 48

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 18

Expert Comment

by:Pawan Kumar Khowal
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 18

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now