0

I have a web page with the next TextBox:

<asp:TextBox ID="txtSum" runat="server"></asp:TextBox>  

and compareValidator to validate it:

<asp:CompareValidator ID="CompareValidator1" runat="server" Display="Dynamic" ControlToValidate="txtSum" ErrorMessage="less than 0" ValueToCompare="0" Type="Double" Operator="GreaterThan">  
</asp:CompareValidator>

when I set Culture of the page to Russian, the compare validation does not work well.
I found the reason is that format number is different between English and Russian.
I tried to change the format as follow:

NumberFormatInfo format = CultureInfo.CreateSpecificCulture("en-US").NumberFormat;  
Thread.CurrentThread.CurrentCulture.NumberFormat = format;

but it does not work. in debug I see the NumberFormat of culture has changed but in the page I get the message less than 0.

how can I solve it?

Rachel Fishbein
  • 818
  • 2
  • 12
  • 29
  • _Well_, when you compare your numeric values, you don't compare them with their string representations of specific cultures. You compare them their real values. That's why, I don't think specify your `CurrentCulture` or not change this validation. The problem might be somewhere else. – Soner Gönül Dec 10 '14 at 07:29
  • I saw that in English the numberFormat.CurrencyDecimalSeparator is "." (dot) and in Russian it is "," (comma), so I'm almost sure that is the reason. (in addition, when I change culture to hebrew I have no problem because the NumberFormat of hebrew is same as English. – Rachel Fishbein Dec 10 '14 at 07:36

1 Answers1

0

Issue you are facing is due to browser culture. Browser culture is Russian and your code execution on IIS might have culture english. Always browser culture is getting passed. You need to parse double value using invariant culture or change current UI culture so that it will work.

protected override void InitializeCulture()
{
    Page.Culture = CultureInfo.CreateSpecificCulture("en-US");
    Page.UICulture = CultureInfo.CreateSpecificCulture("en-US");    
}
Amit
  • 882
  • 6
  • 13
  • of course I did it. before the code in the question I wrote: CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US"); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; (by the way, the TextBox and ComppareValidator are in UserControl ascx) – Rachel Fishbein Dec 10 '14 at 08:12
  • Ok. Can you comment this code and try setting web.config globalization property. – Amit Dec 10 '14 at 12:07
  • did not help. thank you, but I simply replaced compareValidator with , and somehow it works nice. – Rachel Fishbein Dec 11 '14 at 06:16