ASP.NET MVC 3 RC 2 client side validation with globalization
Asked Answered
S

2

11

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!

Stamm answered 17/12, 2010 at 16:51 Comment(0)
C
8

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.

Cabin answered 28/12, 2010 at 12:59 Comment(5)
Your if incorrectly identifies 0 as an invalid value; use return !isNaN(Globalization.parseFloat(value)); as the body of the function.Copse
Did you inform the plugin creators? Would you please do so if not already done. I'm having the same problem as sde but i don't want to "hack" with the unofficial or overwriting methods.Cicatrize
Well, just just saved me a ton of code digging - you're a star, thanks so much for the blog post and sharing this on SO!Siltstone
What if I want to validate BOTH 0.1 and 0,1?Crenel
If you want to validate both then just write your own code in the validator function. I suppose you can use the Globalization plugin to validate a specific culture - just check it out.Cabin
S
1

I had to disable the UnobtrusiveJavaScript. The page does not react instantly anymore, but at least it works.

You can disable by default in the Web.Config.

<appSettings>
    <add key="enableSimpleMembership" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="false"/>
 </appSettings>

And I use the Microsoft scripts for validation:

MicrosoftAjax.js
MicrosoftMvcAjax.js
MicrosoftMvcValidation.js

And also the jquery-1.4.4.min.js & jquery.glob thing, but I think I use them internally. The validation is being done by the MS scripts.

Soy answered 17/12, 2010 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.