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]+$"
Member_2_7967119Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Pawan KumarConnect With a Mentor Database 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:
Hi,

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

Open in new window


Regards
0
 
Rgonzo1971Commented:
I wouldn' t use
@"^[0-9]*$"

since it matches empty strings
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.