Setting client-side validation for German

This is something basic, I think, and the accepted answer will probably be a reference to what I could not find:

I want client-side validation of my application to use German formats for numbers, dates, etc. For the server side, I installed

<globalization uiCulture="de" culture="de-DE" /> 

in the web.config file and display dates, for example. create or details view, I use

 [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)] 

Now the client side still uses dd / MM / yyyy - Format for verification and, accordingly, displays an error message in English.

In MVC4, what is the right way to set client-side validation to a specific culture?

+4
source share
1 answer

Now I use the following:

1) Attaching this (stolen) code somewhere after loading jqueryval, etc. (and the presence of the files to which they refer) will have a culture applied to globalization at the bottom of this code, as a reference for checking input:

 <script type="text/javascript" src="~/Scripts/jquery.globalize/globalize.js"></script> <script type="text/javascript" src="~/Scripts/jquery.globalize/cultures/globalize.culture.de-DE.js"></script> <script type="text/javascript"> (function ($, Globalize) { // Tell the validator that we want numbers parsed using Globalize $.validator.methods.number = function (value, element) { var val = Globalize.parseFloat(value); return this.optional(element) || ($.isNumeric(val)); }; $.validator.methods.min = function (value, element, param) { var val = Globalize.parseFloat(value); return this.optional(element) || val >= param; }; $.validator.methods.max = function (value, element, param) { var val = Globalize.parseFloat(value); return this.optional(element) || val <= param; }; $.validator.methods.range = function (value, element, param) { var val = Globalize.parseFloat(value); return this.optional(element) || (val >= param[0] && val <= param[1]); }; // Tell the validator that we want dates parsed using Globalize $.validator.methods.date = function (value, element) { var val = Globalize.parseDate(value); return this.optional(element) || (val); }; }(jQuery, Globalize)); $(document).ready(function () { // Set Globalize to german Globalize.culture("de-DE"); }); </script> 

2) A custom error message if the value entered in the field violates the check or the user tries to send with an empty value for this field:

 @Html.ValidationMessageFor(model => model.Datum, "my custom error message") 

This is pitti, I know that I am noob, but it should be more intuitive to check server and client validation on a different culture, including all error messages.

0
source

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


All Articles