加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何在JQuery的验证中本地化验证消息?

发布时间:2020-12-16 06:54:05 所属栏目:asp.Net 来源:网络整理
导读:是否存在本地化 JQuery验证消息的标准做法? 通过声明我自己的ClassRules并引用它们而不是默认的ClassRules,我已经能够一起破解. 我的代码. input class="localized-required" id="myTextInput" name="myTextInput" type="text" value="" /script language="
是否存在本地化 JQuery验证消息的标准做法?

通过声明我自己的ClassRules并引用它们而不是默认的ClassRules,我已经能够一起破解.

我的代码.

<input class="localized-required" id="myTextInput" name="myTextInput" type="text" value="" />

<script language="javascript" type="text/javascript">
        jQuery(document).ready(function () {
            $.validator.addMethod("localized-required",$.validator.methods.required,'<%: Resources.Strings_ValidationMessages.SelectionRequired %>');

            $.validator.addClassRules(
            {
                "localized-required": { "localized-required": true }
            });

            jQuery("#myForm").validate();
        })
    </script>

我只是想看看是否有更好的方法.

解决方法

您可以覆盖验证器对象中的消息对象.

$.validator.messages = {
    required: '<%: Resources.Strings_ValidationMessages.SelectionRequired %>'
};

或者您可以使用自己的defaultMessage函数.

$.validator.prototype.defaultMessage = function(element,method) {
    var locale = magicFunctionToGetLocale();
    var message = $.validator.localizedMessages[locale][method];

    return this.findDefined(
        this.customMessage( element.name,method ),this.customMetaMessage( element,// title is never undefined,so handle empty string as undefined
        !this.settings.ignoreTitle && element.title || undefined,message,"<strong>Warning: No message defined for " + element.name + "</strong>"
    );
};

在上面的示例中,$.validator.localizedMessages是在代码中的其他位置创建的对象.标准验证插件没有localizedMessages对象.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读