Can't get the jquery globalization to work
Asked Answered
C

3

19

I try to use the jQuery Globalization plugin in order to fix the comma problem with jquery unobstructive client validation. However I tried many many solutions and there no good solution to fix this. I am on a non-English localization computer and this is important that my customers enter a decimal value like "123,66" and not "123.66". ASP.NET validation tell me that the price must be a number! meh ? are you serious ? lol

I am getting this javascript error when I try to do the fix.

$.global is undefined

Here my code.

Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

    <script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>

    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"> </script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/globalize.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/cultures/globalize.cultures.js")" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/jquery.form.js")"type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/glob.fix.js")" type="text/javascript"></script>
</head>

<body>
    @RenderBody()
</body>
</html>

glob.fix.js

$.validator.methods.range = function (value, element, param) {
   var globalizedValue = value.replace(",", ".");
   return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}

$.validator.methods.number = function (value, element) {
   return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}

I can't understand.. it should work since I added ~/Scripts/globalize.js.

Any idea? or you might have a better solution for having client validation work and lets me enter comma as decimal values?

Catlee answered 9/2, 2012 at 3:5 Comment(1)
Thank you for the range validator code!Devindevina
C
22

I found the way to finally get rid of the decimal problem with comma seperator!

Here a picture of the result! No more validation problems.

enter image description here

The steps to the fix.

1- Get the Globalization library for jQuery

You must get the latest script! Also I found some answers out there that was outdated.
The object to call the library is no more $.global or anything like that but Globalize.

2- Include the scripts in your project. You must add them after jquery.validation stuff.

<script src="@Url.Content("~/Scripts/globalize.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/cultures/globalize.cultures.js")" type="text/javascript"></script>

3- Replace some methods of the validator. This will override the methods for 'number' and 'range' validation which was causing problems.

$.validator.methods.number = function (value, element) {
    return this.optional(element) || !isNaN(Globalize.parseFloat(value));
} 

$.validator.methods.range = function (value, element, param) {
    return this.optional(element) || (Globalize.parseFloat(value) >= param[0] && Globalize.parseFloat(value) <= param[1]);
}

Globalize.parseFloat => This will actually replace anything that contains ',' to '.' if you selected a culture that require it.

After this.. You must add. This will make the globalize functions to work with the culture.

$(document).ready(function () {
        Globalize.culture('fr-CA');

        // Only there to show which culture are being used.
        console.log(Globalize.culture().name);
});

The complete code look like...

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

    <script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"> </script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/globalize.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/cultures/globalize.cultures.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.form.js")"type="text/javascript"></script>


<script type="text/javascript">

    $.validator.methods.number = function (value, element) {
        return this.optional(element) || !isNaN(Globalize.parseFloat(value));
    } 
    $.validator.methods.range = function (value, element, param) {
        return this.optional(element) || (Globalize.parseFloat(value) >= param[0] && Globalize.parseFloat(value) <= param[1]);
    }

    $(document).ready(function () {
        Globalize.culture('fr-CA');

        // Only there to show which culture are being used.
        console.log(Globalize.culture().name);
    });
</script>

</head>

<body>
    @RenderBody()
</body>
</html>
Catlee answered 10/2, 2012 at 0:24 Comment(1)
Where are the Globalize.js file located in the github url?Cullie
W
2

Well, to solve the same problem, I did:

$.validator.addMethod("price",function(value){
    return /^(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
});

and then used the added method as rule:

$("#form").validate({
    rules: {
        price: "price"
    }
});

I "borrowed" the validator regex from the validation plugin itself, and inverted the dots . and the commas , (thousand separator X decimal separator).

Weintraub answered 9/2, 2012 at 3:17 Comment(5)
Where did you put that validate(...) ?Catlee
docs.jquery.com/Plugins/Validation - look at "View Source" of the first Example/DemoWeintraub
I am starting to think that i shouldnt use the unobstructive validation from asp.net mvc but use only the plugin instead. (I know asp.net mvc use that plugin but it seem i don't get control over something which cause me lots of problems)Catlee
Alright, your code cannot work with the unobstructive validation since the validation function is handled by it. I would like to avoid using to write javascript with jquery myself for the validation. But thanks for the try! Ive posted a great solution i found if your interested.Catlee
You don't need to use the validation function. You can add the rule using the class attribute for the HTML input: <input class="price" />Weintraub
J
0

For whatever reason I had to change my globalize reference from:

<script src="@Url.Content("~/Scripts/globalize/globalize.js")" type="text/javascript"</script>

to

<script type="text/javascript" src="~/Scripts/globalize/globalize.js"</script>

And that solved my problem. Went crazy for 30min until I got it to work. If anybody can explain why I will appreaciate.

Jinn answered 14/1, 2016 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.