Asp.net-mvc – ASP.NET MVC 3 RC 2 client side validation with globalization

asp.net-mvcasp.net-mvc-3jquery-globalization

My goal is to validate a user input on the client-side, depending on the users' culture.

I have a primitive data-model with the following structure:

public class User
{
 public int UserId { get; set; }

 [Required]
 [StringLength(20,MinimumLength=3)]
 public string Name { get; set; }

 [Required]
 public double Height { get; set; }
}

Furthermore, I want to have client-side validation enabled, checking if it is a valid number. Therefore, I've added the following lines in the <head> section of my _Layout.cshtml.

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

Since I want to have the ability to validate the input in another language formats (in this particular context it's German with the decimal number format of 0,75 whereas in the US it would be 0.75), I've added the following lines (jQuery Globalization PlugIn) AFTER the previously mentioned jquery.validate.min.js and jquery.validate.unobtrusive.min.js.

<script src="@Url.Content("~/Scripts/jquery.glob.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/globinfo/jquery.glob.de-de.js")" type="text/javascript"></script>
<script type="text/javascript">
 $(document).ready(function () {
  $.culture = jQuery.cultures['de-DE'];
  $.preferCulture($.culture.name);
 });
</script>

In addition, I've added the following line to the web.config in the system.web section just to make sure that the German culture is always selected:

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

Now I am experiencing the following behavior:

  • If I type in 0,1 (note the German 'spelling') in the textbox for the value of "Height", the validation error message The field Height must be a number appears and I'm not able to submit the form.
  • If I type in 0.1 (English 'spelling'), I can submit the form but the server-side validation returns the following validation error message The value '0.1' is not valid for Height.

So now I am in some kind of dead lock where I can't get out.

Again, my goal is to validate the decimal number input on the client AND server side, based on the users' culture (in this case it's forced to be German). What am I doing wrong?

Any help is highly appreciated! Thank you in advance!

Best Answer

Unfortunately there's a naming conflict between jQuery.validate.format and jQuery.Globalization.format functions. Therefore you'll have to change your code to use the non-jquery Globalization plugin.

I just wrote a blog post about it here.

For you it should look like this:

<script src="@Url.Content("~/Scripts/globalization.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/globinfo/Globalization.de-DE.js")" type="text/javascript"></script>
<script type="text/javascript">
$.validator.methods.number = function (value, element) {
    if (!isNaN(Globalization.parseFloat(value))) {
        return true;
    }
    return false;
}
$(document).ready(function () {
    $.culture = jQuery.cultures['de-DE'];
    $.preferCulture($.culture.name);
    Globalization.preferCulture($.culture.name);
});
</script>

That should be enough.