Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello All,

Here I have one problem my text box allowed only integer and float value. if eny thing else will give error message.Please find my code Below.


C#
if (!int.TryParse(x, out value))
                   {
                       ModelState.AddModelError("", "Error");
                   }



Please help me

Thanks in advance...
Posted
Comments
Sergey Alexandrovich Kryukov 17-Jun-15 2:30am    
Your idea is good, it really should be TryParse, only "both integer and float" makes no sense. It should be one certain expected type. Also, you can filter out unwanted characters from input. Sorry, no more detail, because you did not specify what UI library/framework you are using.
—SA

If it should be either a int or a float you could TryParse both and give error only if both fail.
 
Share this answer
 
v2
Regex may help you. Try this Regular Expression:
^\((?=.)([+-]?([0-9]*)(\.([0-9]+))?)\)$


--Amy
 
Share this answer
 
Comments
Kenneth Haugland 17-Jun-15 3:17am    
Should be careful with this, ',' and '.' are both valid as a decimal place depending on the culture on the pc.

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