Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate integer and float input in an ASP.NET textbox

I am using the code below to validate integers and floats in ASP.NET, but if I do not enter decimal then it gives me an error.

 <asp:TextBox ID="txtAjaxFloat" runat="server" />
 <cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" TargetControlID="txtAjaxFloat" FilterType="Custom, numbers" ValidChars="." runat="server" />

I also have the regular expression from What's a C# regular expression that'll validate currency, float or integer?, but it's giving a validation error if I enter only one value after the decimal..

like image 395
Dr. Rajesh Rolen Avatar asked Oct 24 '25 09:10

Dr. Rajesh Rolen


2 Answers

Use ControlValidators.

For example (from the link)

<asp:textbox id="textbox1" runat="server"/>
<asp:RangeValidator id="valRange" runat="server"
    ControlToValidate="textbox1"
    MaximumValue="12/31/1998"
    MinimumValue="1/1/1998"
    Type="Date"
    ErrorMessage="* The date must be between 1/1/1998 and 12/13/1998"
    Display="static">*</asp:RangeValidator>
>

The Type attribute can be one of "String", "Integer", "Double", "Date" or "Currency"

like image 110
Dead account Avatar answered Oct 26 '25 03:10

Dead account


You can try the following.

<asp:TextBox ID="TextBox2" runat="server" Style="z-index: 103; left: 289px; position: absolute; top: 132px"></asp:TextBox>
<cc1:FilteredTextBoxExtender
    ID="FilteredTextBoxExtender1"
    runat="server"
    TargetControlID="TextBox2"
    ValidChars="0123456789.">
</cc1:FilteredTextBoxExtender>
like image 24
kranthi Avatar answered Oct 26 '25 05:10

kranthi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!