MVC 5 Validation en-BR client side

4

I am having difficulty performing date and number validation on the client side using MVC 5 asp.net. Examples and tutorials that I find on the internet are all outdated.

I made the modifications as this

asked by anonymous 27.05.2017 / 16:13

1 answer

0

You should respect this sequence:

1 - jquery.unobtrusive-ajax.js // If you have Ajax

2 - jquery.validate.js

3 - jquery.validate.unobtrusive.js

4 - globalize.js

5 - jquery.validate.globalize.js

6 - methods-en.js // This you will create and insert by penultimate

7 - helper.js // This you will create and enter last

Add the following code in methods-pt.js

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

Add the following code in helper.js:

$(document).ready(function () {

    //Filtro Decimal para aceitar vírgula
    $.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);
    }
});
    
27.05.2017 / 17:17