Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have an textbox which accepts numeric and decimal value.

So when the user enters 0, it will display an alert message on click of ok button.
An alert should be displayed even if the user enters any decimal value along with 0 such as

0.-- valid
00.-- valid
000.-- valid
0.0-- valid
00.0-- valid
etc

so i used Convert.ToDecimal(plannedHours) == 0

but now the problem is am not able to check if only decimal value is entered in the textbox. since when only decimal point is entered, its trying to convert a decimal point into a decimal because of the above condition.

pleease help me?
Posted
Comments
ArunRajendra 12-Mar-14 4:00am    
Do you mean if the user enters only "."? What should be the behavior if user enters only "."?

Don't use Convert.ToDecimal - use decimal.TryParse instead.
It returns a bool value which is true if the conversion worked (I.e. it was a valid number)
 
Share this answer
 
So the problem is when user enter"00." the it throw error in conversion,right ?

The solution is you can check is string comming from text box contains "." or not.
Ypu can accomplish this using

if(planedHousrs.Contains('.'))
{
//Here goes you next if conidtion
if(Convert.ToDecimal(plannedHours) == 0)
{

}
}

Alternative:
if(planedHousrs.Contains('.'))
{
//Here just add 0 in last as"0" after "." doesn't effect you value but it will fix you error
plannedHousrs = plannedHousrs + "0";

}

//Here you next conidtion
Convert.ToDecimal(plannedHours) == 0
 
Share this answer
 
Comments
Member 10593922 12-Mar-14 7:14am    
thank u.. it was help full for me..
but instead of contains i used if (txtPlannedHours.Text.EndsWith("."))

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