ASP.NET MVC 3 RC 2 全球化客户端验证
我的目标是根据用户的文化在客户端验证用户输入。
我有一个具有以下结构的原始数据模型:
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.js
和 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>
此外,我还在 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 messageThe 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 messageThe 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,jQuery.validate.format 和 jQuery.Globalization.format 函数之间存在命名冲突。因此,您必须更改代码才能使用非 jquery 全球化插件。
我刚刚在此处写了一篇关于它的博客文章。
对你来说,它应该是这样的:
这就足够了。
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:
That should be enough.
我不得不禁用 UnobtrusiveJavaScript。该页面不再立即做出反应,但至少它可以工作。
您可以在 Web.Config 中默认禁用。
我使用 Microsoft 脚本进行验证:
还有 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.
And I use the Microsoft scripts for validation:
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.