ASP.NET MVC与jQuery验证 - 信息定制和本地化 [英] ASP.NET MVC with jQuery Validation - messages customization and localization

查看:103
本文介绍了ASP.NET MVC与jQuery验证 - 信息定制和本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地化框架支持ASP.NET MVC(4)项目。我应该改变浏览器设置为另一种语言,框架会自动选择合适的资源文件。

不过,因为我用我knockoutjs回落到jQuery验证那些意见。不幸的是有本地化有没有自动支持。

我的问题是 - 什么是最好的做法,以及如何定制和本地化jQuery验证消息,因此他们将与所有MVC资源一起自动拿起

东西jQuery验证消息将以类似的方式给定的数据注释信息资源和信息的ID的行为。

在特定的 -


  1. 如何我做jQuery的拿起消息的我想从资源,而不是它的默认的这个字段是必须的,所以它会打印类似请输入电子邮件和


  2. 我怎样才能让jQuery的打印相同的定制信息的用另一种语言自动的我应该改变浏览器的语言?


感谢您提前。


解决方案

  

1)我如何让jQuery的拿起我从资源,而不是其默认该字段是必需的所需信息,所以它会打印类似请输入电子邮件。


在任何时间被称为过度骑消息下面。下面的字符串可以使用变量替换。

  jQuery.extend(jQuery.validator.messages,{
    要求:该字段是必需的。
    遥控器:请解决这个领域。
    电子邮件:请输入一个有效的电子邮件地址。
    网址:请输入一个有效的URL。
    日期:请输入一个有效的日期。
    dateISO:请输入有效日期(ISO)。
    数字:请输入有效的数字。
    数字:请只输入数字。
    的信用卡:请输入一个有效的信用卡号码。
    equalTo:请再次输入相同的值。
    接受:请使用有效的扩展输入一个值。
    MAXLENGTH:jQuery.validator.format(请输入不超过{0}个字符。)
    MINLENGTH个:jQuery.validator.format(请至少{0}个字符输入。)
    rangelength:jQuery.validator.format(请输入{0}和{1}个字符之间的值。)
    范围:jQuery.validator.format(请输入之间的值{0}和{1}),
    最大:jQuery.validator.format(请输入小于或等于{0}的值。),
    分:jQuery.validator.format(请输入大于或等于一个值{0})。
});

另外,你可以在 .validate声明你的规则()时,你得到更具体的

  $(文件)。就绪(函数(){    $('#MyForm的')。验证({//初始化插件
        规则:{
            字段1:{
                要求:真实,
                MINLENGTH个:5
            }
        },
        消息:{
            字段1:{
                要求:对于必填字段1自定义消息,
                MINLENGTH到:自定义消息:{0}个字符需要
            }
        }
    });});

DEMO: http://jsfiddle.net/XV3ZR/

要的动态的更改任何信息的之后的插件首先被初始化,需要使用的the 规则('加')方法

  $('#字段1)。规则(添加,{
    消息:{
        要求:字段1要求的,
        MINLENGTH到:{0}个字符需要
    }
});

演示2: http://jsfiddle.net/PJGgE/1/


  

2)如何让jQuery的另一种语言打印相同的定制信息自动我应该改变浏览器的语言?


我不知道你所说的改变浏览器语言的意思,但同样,在#1上述方法是唯一的办法,据我所知。这些只是字符串,你必须给他们手动或通过外面的方法进行翻译。

I have ASP.NET MVC (4) project which localization is supported by the framework. Should I change browser settings to another language, framework automatically picks up the right resource file.

However, because I am using knockoutjs I fall back to jQuery validation at those views. Unfortunately there's no automatic support for localization there.

My question is - what are the best practices and ways to customize and localize jQuery validation messages so they will be picked automatically together with all MVC resources?

Something that jQuery validation messages will behave in a similar manner to Data Annotations messages given resources and message Ids.

In particular -

  1. How to I make jQuery pick up the message I want from the resources instead of its default "This field is required", so it will print something like "Please enter email" and

  2. How can I make jQuery print the same customized message in another language automatically should I change browser language ?

Thank you in advance.

解决方案

1) How to I make jQuery pick up the message I want from the resources instead of its default "This field is required", so it will print something like "Please enter email".

The following is called to over-ride messages at any time. The strings below can be replaced with variables.

jQuery.extend(jQuery.validator.messages, {
    required: "This field is required.",
    remote: "Please fix this field.",
    email: "Please enter a valid email address.",
    url: "Please enter a valid URL.",
    date: "Please enter a valid date.",
    dateISO: "Please enter a valid date (ISO).",
    number: "Please enter a valid number.",
    digits: "Please enter only digits.",
    creditcard: "Please enter a valid credit card number.",
    equalTo: "Please enter the same value again.",
    accept: "Please enter a value with a valid extension.",
    maxlength: jQuery.validator.format("Please enter no more than {0} characters."),
    minlength: jQuery.validator.format("Please enter at least {0} characters."),
    rangelength: jQuery.validator.format("Please enter a value between {0} and {1} characters long."),
    range: jQuery.validator.format("Please enter a value between {0} and {1}."),
    max: jQuery.validator.format("Please enter a value less than or equal to {0}."),
    min: jQuery.validator.format("Please enter a value greater than or equal to {0}.")
});

Otherwise, you can get more specific when declaring your rules within .validate().

$(document).ready(function () {

    $('#myform').validate({ // initialize the plugin
        rules: {
            field1: {
                required: true,
                minlength: 5
            }
        },
        messages: {
            field1: {
                required: "custom message for field 1 required",
                minlength: "custom message: {0} chars required"
            }
        }
    });

});

DEMO: http://jsfiddle.net/XV3ZR/

To dynamically change any messages after the plugin is first initialized, requires an over-ride by using the rules('add') method.

$('#field1').rules('add', {
    messages: {
        required: "field 1 required",
        minlength: "{0} chars required"
    }
});

DEMO 2: http://jsfiddle.net/PJGgE/1/

2) How can I make jQuery print the same customized message in another language automatically should I change browser language ?

I'm not sure what you mean by "change browser language", but again, the methods in #1 above are the only ways, AFAIK. These are just strings and you'll have to translate them manually or via outside methods.

这篇关于ASP.NET MVC与jQuery验证 - 信息定制和本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆