0

I have a very basic scenario:

In my model:

public Double MyNumber { get; set; }

(no annotations so far)

In my view:

<input type="hidden" id="locale" value="@System.Threading.Thread.CurrentThread.CurrentUICulture"/>

@Html.LabelFor(model => model.MyNumber)
@Html.EditorFor(model => model.MyNumber)
@Html.ValidationMessageFor(model => model.MyNumber)

In my javascript code on the same page:

<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="/Scripts/cldr.js"></script>
<script src="/Scripts/cldr/event.js"></script>
<script src="/Scripts/cldr/supplemental.js"></script>
<script src="/Scripts/globalize.js"></script>
<script src="/Scripts/globalize/number.js"></script>
<script src="/Scripts/globalize/date.js"></script>
<script src="/Scripts/jquery.validate.globalize.js"></script>

<script>
    $.when(
        $.getJSON("/Scripts/cldr/supplemental/likelySubtags.json"),
        $.getJSON("/Scripts/cldr/main/" + $("#locale").val() + "/numbers.json"),
        $.getJSON("/Scripts/cldr/supplemental/numberingSystems.json"),
        $.getJSON("/Scripts/cldr/main/" + $("#locale").val() + "/ca-gregorian.json"),
        $.getJSON("/Scripts/cldr/main/" + $("#locale").val() + "/timeZoneNames.json"),
        $.getJSON("/Scripts/cldr/supplemental/timeData.json"),
        $.getJSON("/Scripts/cldr/supplemental/weekData.json")
        ).then(function () {
            return [].slice.apply(arguments, [0]).map(function (result) {
                return result[0];
            });
        }).then(Globalize.load).then(function () {
            Globalize.locale($("#locale").val());
        }).then(console.log("LOADED EVERYTHING"));
</script>

I get no errors when loading the page, so i presume that the Globalize initialisation is successful

Now, the problem is (when using nl-BE culture):

  • Both 1.23 and 1,23 are accepted by the client-side validation.
  • Only 1,23 should be accepted according to nl-BE culture.
  • Server side validation (.NET) works as expected, and rejects the 1.23.

Do I need to add annotations? I presume that by default no annotations are needed for basic validation.

Am I doing something wrong with the JQuery.Globalize validation? (I must say, the 0.x version was waaaay more easy to use. This 1.x version with CLDR data is so complex when you don't want to use npm or bower, but only nuget)

For clarity, these are my expectations, but maybe I expect too much

  • The user can switch between different cultures. Some of those cultures expect 3.14 for PI, some expect 3,14 for PI
  • In a culture where the "." is the decimal separator, i want the client side validation to accept only "3.14", and show an error when "3,14" is entered.
  • In a culture where the "," is the decimal separator, i want the client side validation to accept only "3,14", and show an error when "3.14" is entered.
  • This way, client and server side validation are synced.
  • Somehow, I expected JQuery.Globalize to do this for me.
  • For the moment, client side validation accepts both, but server side validation only accepts the correct one.

How can I resolve this?

StevenQ
  • 182
  • 1
  • 11
  • The problem here is that JavaScript doesn't care about the `.` or `,` notation if you `parseInt()` the value. It both works. You might want to do a `string.replace()` there. – Randy Oct 19 '16 at 09:45
  • I'm not using parseInt or any other explicit javascript code on the fields. I expect JQuery Globalize to validate the field, in an "unobtrusive" way. – StevenQ Oct 19 '16 at 09:57
  • You don't use it, but what I meant was that JavaScript doesn't really care about `,` or `.`, it works the same for the interpreter. – Randy Oct 19 '16 at 10:07

0 Answers0