views:

30

answers:

1

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 number 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?

A: 

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
Jason Berkan