views:

2746

answers:

4

Hi,

I am using a custom validator to compare value in two text box. This is comparing the values fine. But it says "025" and "25" are different.. can this do a float comparision.

the custom validator i am using is

<asp:CompareValidator id="compval" runat="server" ControlToValidate="txtBox1"
        ErrorMessage="There values are not equal."
        Enabled="False" ControlToCompare="txtBox2">*</asp:CompareValidator></TD>

Please let me know if this is possible.

A: 

use a compare validator with a type of int?

Andrew Bullock
+1  A: 

Use System.Double.Parse(value) to convert both to a floating point number, and compare those numbers.

You can also use TryParse if you don't want to handle exceptions if the value is not a valid floating point number.

See also:

Wimmel
+1  A: 

The only thing I can think of without seeing your validation code is that 025 is being interpreted as an octal number (in C, putting a zero before an integer means it's in base 8). Then 025 would be 21 in base-10 and your two numbers wouldn't be the same.

I'm not sure how you'd come up with that, though. I tested out a few of the Parse() functions and they all convert the string "025" to base-10 25.

Dana Robinson
A: 

I guess the following is what you need (the question could be phrased a little clearer)

<asp:CompareValidator ID="cv1" runat="server" ControlToCompare="txt1" ControlToValidate="txt2" Operator="Equal" Type="Integer" ErrorMessage="integers in txt1 and txt2 are not equal" />
Stefan