views:

668

answers:

4

I am using below code to validate interger and float in asp.net but if i not enter decimal than it give me error

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

i have this regex also but its giving validation error if i enters only one value after decimal.. http://stackoverflow.com/questions/617826/whats-a-c-regular-expression-thatll-validate-currency-float-or-integer

A: 

How about plain simple parsing? E.g.

int i;
if (!int.TryParse(txtAjaxFloat.Text, out i))
   i = 0;

float f;
if (!float.TryParse(txtAjaxFloat.Text, out f))
   f = 0;

Where 0 is your default "could not validate" value.

adrianos
+2  A: 

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"

Dead account
A: 

hi you can try this suppose it will give result make it vote

<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>
kranthi
A: 

i M USING THIS FOR FLOAT VALIDATION

cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" TargetControlID="txtAjaxFloat" FilterType="Custom, numbers" ValidChars="." runat="server"

WHEN I APPLY ON THIS ON TEXT BOX ITS ALSO TAKE VALUE LIKE 123.12.12 SO ITS NOT POSSIBLE TO TAKE LIKE THIS TYPE OF VALUE AS FLOAT WAT SHOULD I DO

VISHAL