ASP.NET MVC 3 RC 2 全球化客户端验证

发布于 2024-10-07 19:40:05 字数 2003 浏览 5 评论 0原文

我的目标是根据用户的文化在客户端验证用户输入。

我有一个具有以下结构的原始数据模型:

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

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

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

此外,我想启用客户端验证,检查它是否是有效的数字。因此,我在 _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>

因为我希望能够验证另一种语言格式的输入(在这个特定的上下文中,它是德语,十进制数字格式为 0,75 而在美国则为 0.75< /code>),我添加了以下行(jQuery 全球化插件)在前面提到的 jquery.validate.min.jsjquery.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>

此外,我还在 system.web 部分的 web.config 中添加了以下行,以确保始终选择德国文化

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

:遇到以下行为:

  • 如果我在“Height”值的文本框中输入 0,1(注意德语“拼写”),则会出现验证错误消息 The field Height must be出现一个数字,但我无法提交表单。
  • 如果我输入 0.1(英语“拼写”),我可以提交表单,但服务器端验证会返回以下验证错误消息 值“0.1”对于高度无效。

所以现在我陷入了某种无法摆脱的僵局。

同样,我的目标是根据用户的文化(在本例中强制为德语)验证客户端和服务器端输入的十进制数字。我做错了什么?

非常感谢任何帮助!先感谢您!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疑心病 2024-10-14 19:40:05

不幸的是,jQuery.validate.format 和 jQuery.Globalization.format 函数之间存在命名冲突。因此,您必须更改代码才能使用非 jquery 全球化插件。

我刚刚在此处写了一篇关于它的博客文章。

对你来说,它应该是这样的:

<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>

这就足够了。

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.

听风念你 2024-10-14 19:40:05

我不得不禁用 UnobtrusiveJavaScript。该页面不再立即做出反应,但至少它可以工作。

您可以在 Web.Config 中默认禁用。

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

我使用 Microsoft 脚本进行验证:

MicrosoftAjax.js
MicrosoftMvcAjax.js
MicrosoftMvcValidation.js

还有 jquery-1.4.4.min.js & jquery.glob 的东西,但我想我在内部使用它们。验证由 MS 脚本完成。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文