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

I have a textbox in that I have to validate integer or float in asp.net.

Please let me know, how to implement it?

Thanking You
Mohd. Wasif
Posted

 
Share this answer
 
Comments
Espen Harlinn 15-Apr-11 8:23am    
Good links, 5ed!
Ashishmau 18-Apr-11 0:50am    
Thanx
I took from your question that the validation is to be done on the server side so try it like this:
Depends on what kind of validation you need. First make sure the text you received in said fields is really and integer or a double (don't use floats as they don't have any advantage over doubles). This can be achieved by using double.TryParse(String strVal, out double value) or int.TryParse(String strVal, out int value) methods.
When the conversion was successful you can check for containment in a range or an upper or lower bound etc.

If you have any doubts leave me a comment!

-MRB
 
Share this answer
 
v2
You can take a help of Javascript
Here is the way to validate click ME[^]
 
Share this answer
 
You have to use CompareValidator like below

<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="intValidator" runat="server" ControlToValidate="txt1" Operator="DataTypeCheck" Type="Double" ErrorMessage="Value must be a integer or float" />

OR

You can use RegularExpressionValidator and write the regular expression to validate number and float values.

OR

You can use CustomValidator and write your own Javascript to validate number and float values

This should solve your problem.
 
Share this answer
 
v3

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