Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi,everyone

Question: i have one text-box and i want to allow only numeric value to this text-box like i.e .2,2.3,23.233

Solution: For the above, i already try regex ("^\s*(?=.*[1-9])\d*(?:\.\d{1,4})?\s*$")
but still it can`t be allow after point value like example(".2")

so, i need to know that their are any other solutions available .....please help me to get-out of it
Posted
Comments
Sergey Alexandrovich Kryukov 26-May-15 9:59am    
Why Regex?
What are you trying to achieve, ultimately?
—SA

Try this "\d+(\.\d+)?". You can find plenty of examples over the net. Just Google!
 
Share this answer
 
Better use
C#
double val;
if (double.TryParse(YourTextbox.Text, out val))
{
    //OK
}
else
{
    //not numeric
}

That strategy will also cope with different regional settings (what about a decimal comma instead of a decimal point?).
 
Share this answer
 
Comments
[no name] 27-May-15 3:53am    
My dear friend Brenhard hiller,

At some point your consult is right but i want to allow point decimal value like(.2)in my textbox and i only use reg-ex using data annotation with mvc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900