Question

I am sorry for my simple question but I can't get it work . How I can make this jquery validator method to work globally . I have this code in my layout:

jQuery.validator.methods.number = function (value, element) {
        return this.optional(element) || !isNaN(Globalize.parseFloat(value));
    };
    jQuery(document).ready(function () {
        jQuery(function () {
            Globalize.culture("ro-RO");
        });
    });

But this is not working in my partial views so to make this code to work I have to put this in all my partial views ...

<script src="@Url.Content("~/Scripts/Common/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/Common/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script>
    jQuery.validator.methods.number = function (value, element) {
        return this.optional(element) || !isNaN(Globalize.parseFloat(value));
    };
    jQuery(document).ready(function () {
        jQuery(function () {
            Globalize.culture("ro-RO");
        });
    });
</script>

    @using (Ajax.BeginForm("CreateSemifabricat_AddResourceToProduct", "Product", null,
        new AjaxOptions{
            HttpMethod = "POST",
            InsertionMode = InsertionMode.Replace,

        }, new { id = "addResToProd" }))
    {

      <input type="submit" value"Submit"/>

    }

So my question is how can I make this code to work globally? Thanks!

Was it helpful?

Solution

You could override the jQuery.validate.js script, but it will add permanent dependency to globalize.js

find this:

return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);

and replace with this:

return this.optional(element) || !isNaN(Globalize.parseFloat(value));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top