18

我有一个使用 jquery mobile 实现的注册表单。用户可以选择用德语、英语或法语填写此表格 - 有多个内部页面用于以每种语言显示表格.. 用户通过菜单按钮选择他们喜欢的语言。

我正在使用 jquery validate 插件来处理客户端表单验证。这很好用.. 我想知道如何在每个语言页面上为表单启用正确的错误消息?当您从 github 下载插件时,它包含所有本地化错误代码,但我不确定如何启用它..

这是我用来处理实现每个语言页面的表单验证的 JavaScript 代码。

感谢您能帮助我为德语和法语页面启用正确的错误消息..

$(document).on("pageshow", "#page_deutsch", function() {

$("#register_deutsch").validate();

});

$(document).on("pageshow", "#page_english", function() {

$("#register_english").validate();

});

$(document).on("pageshow", "#page_francais", function() {

$("#register_francais").validate();

});
4

2 回答 2

29

jQuery validation plugin localization The default language for jQuery validation plugin is English. It could be overridden with the localization js file, eg. es, zh, fr, etc. Please refer here for the list of supported languages.

To enable languages support, we just need to add other language message js into our webpage (html/jsp/xhtml). for french

<script type="text/javascript" src="http://jzaefferer.github.io/jquery-validation/localization/messages_fr.js" />

for chinese

<script type="text/javascript" src="http://jzaefferer.github.io/jquery-validation/localization/messages_zh.js" />

Same for other languages, just find the appropriate language js and add the js into the webpage.

if the required language is not in the list, an workable alternative is to download a copy of the messages_xx.js into project workspace, then modify it to your required language.

Although the language js is provided, but the problem is in 1 webpage we can only use 1 type of language message. If multiple language message js in the webpage, the last will be used.

To resolve this problem, we can dynamically load the messages js by system locale.

<script type="text/javascript" src="http://jzaefferer.github.io/jquery-validation/localization/messages_<%= Locale.getDefault().getLanguage() %>.js" />

with the above solution, finally we can dynamically handle different language message by different system locale.

There is another way to handle multiple languages with resource bundle here.

related jQuery validation articles:

  • Email
  • Credit card
  • Multiple form fields
  • Error message customization
  • Using jQuery Validation Plugin in JSF
  • Multilingual error messages
  • Error handling in jQuery Validation Plugin

Done!!

于 2013-10-09T22:19:59.550 回答