In How to customize error messages for jQuery validation plugin guides us customize error messages for jQuery validation plugin.
But nowadays, multilingual is a requirement for most of the application.
Thus on top of customized messages, we need to provide multilingual support for the error messages.
This article will show an easy way to provide multilingual support for jQuery validation plugin.
The trick is to keep our customized error messages in resource bundle.
Steps:
1. write the customized error messages in our application's resource bundle. eg, messages.properties
e.g.
2. use the resource key for the customized error message
3. prepare the resource bundle in different languages, eg. messages_fr.properties, messages_zh.properties, etc.
4. when system locale changed, it will automatically retrieve the messages from appropriate resource bundle.
Done!!
But nowadays, multilingual is a requirement for most of the application.
Thus on top of customized messages, we need to provide multilingual support for the error messages.
This article will show an easy way to provide multilingual support for jQuery validation plugin.
The trick is to keep our customized error messages in resource bundle.
Steps:
1. write the customized error messages in our application's resource bundle. eg, messages.properties
e.g.
invalid_email=Invalid email format
2. use the resource key for the customized error message
<script>
$(document).ready(function(){
$("#myform").validate({
rules: {
'myform:emailAddr': {
required: true,
email: true
}
},
messages: {
'myform:emailAddr': {
email: "#{msgs.invalid_email}"
}
}
});
});
</script>
3. prepare the resource bundle in different languages, eg. messages_fr.properties, messages_zh.properties, etc.
4. when system locale changed, it will automatically retrieve the messages from appropriate resource bundle.
related jQuery validation articles:
- Email
- Credit card
- Multiple form fields
- Error message customization
- Using jQuery Validation Plugin in JSF
- jQuery Validation Plugin localization
- Error handling in jQuery Validation Plugin
- Credit card
- Multiple form fields
- Error message customization
- Using jQuery Validation Plugin in JSF
- jQuery Validation Plugin localization
- Error handling in jQuery Validation Plugin
Done!!
This doesn't work is the script is located in a separate JS file. It outputs it as string. Any ideas?
ReplyDeleteSorry, I do not get your problem, can you explain in more details.
DeleteHi Kian,
DeleteSorry had a typo and could not edit what I wrote.
I meant to say that putting the resource key "#{msgs.invalid_email}" for the message doesn't render it properly on the browser as "Invalid email format" instead, it's rendering it as a string, so when the validation kicks in. It reads is as "#{msgs.invalid_email}" on the browser and not "Invalid email format".
I have the script as a separate Javascript file and not within the HTML page.
Hi John Mark,
DeleteWhen I moved the javascript into a separate js file as you said, I had the same problem.
I have no ideas at the moment how to resolve this issue as well.
Please update this blog post when you do find the solution. I find your site a great resource! :)
DeleteIn either case, if your target group doesn't get the message, you've likely sent an inappropriate one. Also, that can have critical results.
ReplyDeletewhatsapp gb
I am facing the issue in WPML plugin of wordpress, then I installed Yo WhatsApp which fixes all the error.
ReplyDeleteThanks for sharing this, it was helpful. Keep sharing such content in future. Also checkout Nulls Clash.
ReplyDeleteThe information you have shared here is very helpful. I really like and appreciate your work. Thanks again for it, keep us updated in future!
DeleteThanks you for sharing this tip.
ReplyDeleteThanks Admin FOr Sharing This GB Whatsapp Download.
ReplyDeleteThanks for the detailed article on this topic. I would like to see more such awesome articles from you.
ReplyDeleteThanks for sharing this valuable piece of information. Keep sharing more such awesome articles in the future. Goodbye!
ReplyDeleteYour site is the best source of information, it was very helpful. Thanks for sharing.
ReplyDeleteYour site is the best source of information, it was very helpful. Thanks for sharing. Keep sharing such things.
ReplyDeletethanks for share this detailed article on notes of my java career,Can you tell me more about this via yo whatsapp.
ReplyDeleteHey, thank you a lot for sharing this article with us. I can’t say how grateful we are to read this. Also, I would love to share it with my friends and family, who are interested.
ReplyDeleteI am really extremely glad to visit your blog. Keep doing awesome! Easy Way to Become An Administrator on Roblox, also Forget the password of your existing account? Without wasting time, get back your lost password using Roblox Password Finder. Let's move forward with the solution.
ReplyDelete