Jquery globally parseFloat validation problems

I am trying to use the jquery globalization plugin (https://github.com/jquery/globalize) to work with unobtrusive asp.net mvc3 client side validation. I got attached to the en-CA and fr-CA cultures and saw some discrepancies between what the plugin accepts as a real number, and not what the validation accepted on the service side. Here are some examples of calling the Globalize.parseFloat method with different inputs with two cultures and what I get (bold indicates that server side validation does not validate the number) FR-CA

  • Globalize.parseFloat ("7.12", 10, "fr-CA") returns 7.12
  • Globalize.parseFloat ("7..12", 10, "fr-CA") returns NaN
  • Globalize.parseFloat ("7,12", 10, "fr-CA") returns 7.12
  • Globalize.parseFloat ("7, 12", 10, "fr-CA") returns NaN
  • Globalize.parseFloat ("7 1 2.12", 10, "fr-CA") returns 712.12

en-sa

  • Globalize.parseFloat ("7.12", 10, "en-CA") returns 7.12
  • Globalize.parseFloat ("7..12", 10, "en-CA") returns NaN
  • Globalize.parseFloat ("7,12", 10, "en-CA") returns 712
  • Globalize.parseFloat ("7, 12", 10, "en-CA") returns 712
  • Globalize.parseFloat ("7, 1, 2.12", 10, "en-CA") returns 712.12

The parseFloat code execution looks like it is the intended output, but I cannot figure out how it is intended, so I hope that I have something missing ... or is it intended?

thanks

+6
source share
3 answers

This seems to be a known issue in the globalization plugin (see https://github.com/jquery/globalize/issues/46 ). It looks like I will have to run my own regular expression to make sure that it is in the correct format for client-side validation (well, I need to deal with only two languages ​​at the moment :)

+1
source

Since parseFloat is inside, it starts first, wrapped in Globalize. parseFloat is not designed to handle complex strings.

0
source

The client-side parseFloat method simply ignores thousands of separators, which is why Globalize.parseFloat ("7, 1, 2.12", 10, "en-CA") returns 712.12 (the thousands separator in this culture is ",").

The thousands separator for fr-CA culture is space, so Globalize.parseFloat ("7 1 2,12", 10, "fr-CA") returns 712.12.

A decimal point can occur only once. For en-CA it is ".", For fr-CA it is ",". Thus, all examples containing more than one decimal point will return NaN.

The only thing I can’t explain is why Globalize.parseFloat ("7.12", 10, "fr-CA") returns 7.12. This is strange because neither the decimal point nor the thousands separator are "." in this culture, therefore it should return NaN.

0
source

Source: https://habr.com/ru/post/896923/


All Articles