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

时光总嘲笑我的痴心妄想 提交于 2019-12-03 07:28:14

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4472821/asp-net-mvc-3-rc-2-client-side-validation-with-globalization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!