开发者

CompareValidator currency check does not work with French formatted numbers

开发者 https://www.devze.com 2023-01-19 01:56 出处:网络
I have a salary TextBox and an associated CompareValidator, which is setup as follows: <asp:CompareValidator ... Operator=\"DataTypeCheck\" Type=\"Currency\" />

I have a salary TextBox and an associated CompareValidator, which is setup as follows:

<asp:CompareValidator ... Operator="DataTypeCheck" Type="Currency" />

I fill the TextBox with a formatted string out of the database:

txtSalary.Text = myObject.Salary.ToString("N2")

When a user accesses the page using a French culture (such as fr-ca), the ToString method will put 80 000,00 in the textbox, which is fine.

However, any n开发者_如何学Goumber with a space in it causes the validator to fail, which is not fine. Is there any way to make the CompareValidator work properly with non-US formatted numbers?


I had a problem like you, but not quite the same, I had something like this:

<asp:RangeValidator ID="rw" ErrorMessage="error" 
    Text="!" ControlToValidate="r" MinimumValue="1 000,00" MaximumValue="1 000 000,00" Type="Currency" CultureInvariantValues="false" runat="server" EnableClientScript="true" />;

I databound my controls with data for example 2 000,00 and I had validation error

but when I entered a value od 2 000,00 everything was OK.

the answer was space in CurrencyGroupSeparator, my culture pl-pl has space in it, but it is not space "\x0020" but it is non breaking space "\00A0"

I've used reflector to do some digging and what I found is puzzling

currency format check is in BaseCompareValidator class in method private static string ConvertCurrency(string text, NumberFormatInfo info)

and in code there is a line like this:

if (currencyGroupSeparator[0] == '\x00a0')
{
    currencyGroupSeparator = " ";
}

I putted decompiled code in test project and tried to run it, and indeed code was not working properly.

ConvertCurrency(10000000.00m.ToString("n"), NumberFormatInfo.CurrentInfo) returned null;

why someone put it there I don't know, but then I commented it, method have started work properly.

we cant compile .net framework from source yet, so what we can do, is to change separator from non breaking space to space

so the solution to our problem is:

Thread.CurrentThread.CurrentCulture = new CultureInfo("some culture name"); Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyGroupSeparator = "\x0020"; Thread.CurrentThread.CurrentCulture.NumberFormat.NumberGroupSeparator = "\x0020";


I guess it is just a bug with the CompareValidator and RangeValidator - likely on the client side JavaScript.

I fixed the problem by changing to a CustomValidator, with the following code on the server side (and no code on the client side):

Protected Sub ValidateSalary(ByVal sender As Object, _ 
                             ByVal e As ServerValidateEventArgs)
    Try
        If txtSalary.Text <> String.Empty Then
            Dim salary As Decimal = Convert.ToDecimal(txtSalary.Text, _ 
                Thread.CurrentThread.CurrentUICulture)
        End If

        e.IsValid = True
    Catch ex As Exception
        e.IsValid = False
    End Try
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消