Regex 带有RegularExpression和ErrorMessage的Asp.Net MVC 4.0模型本地化

Regex 带有RegularExpression和ErrorMessage的Asp.Net MVC 4.0模型本地化,regex,asp.net-mvc,model-view-controller,resources,Regex,Asp.net Mvc,Model View Controller,Resources,如何创建从资源文件获取RegularExpression和ErrorMessage的自定义RegularExpressionValidator [RegularExpression(@"\d{5}(-\d{4})?", ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "regExpValforPostal_ErrorMessage")] public string P

如何创建从资源文件获取RegularExpression和ErrorMessage的自定义RegularExpressionValidator

 [RegularExpression(@"\d{5}(-\d{4})?",
          ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "regExpValforPostal_ErrorMessage")]
        public string PostalCode { get; set; }

如果您的资源文件名为Validations.he.resx 其中既有“RegexExpression”又有“ErrorMessage”,您应该使用:

更新#1:添加选项2

  • 备选案文1:

  • 备选案文2:

然后您将像这样使用它:

[LocalizedRegexAttribute]
public string PostalCode { get; set; }

如果您的资源文件名为Validations.he.resx 其中既有“RegexExpression”又有“ErrorMessage”,您应该使用:

更新#1:添加选项2

  • 备选案文1:

  • 备选案文2:

然后您将像这样使用它:

[LocalizedRegexAttribute]
public string PostalCode { get; set; }
资源文件名是全局的:

Global.resx,Global.zh.resx,Global.fr-ca.resx

在模型类中,需要通过自定义数据传递3个参数 注释如下:

资源文件名是全局的:

Global.resx,Global.zh.resx,Global.fr-ca.resx

在模型类中,需要通过自定义数据传递3个参数 注释如下:


我试过了,但没用。regExpValforPostal_ValidationExpression\d{5}(-\d{4})?对于我们ZipCode,我添加了另一个选项,只需自己实现regex。我尝试了,但没有成功。RegepValforPostal_ValidationExpression\d{5}(\d{4})?对于我们ZipCodeI已经添加了另一个选项,只需自己实现正则表达式。如果像这个私有静态字符串LoadRegex(类型resourceType,字符串键)一样将_ErrorMessageResourceType传递给LoadRegex,那么可以使用var resourceManager=new resourceManager(resourceType)并且不必硬编码资源如果您将_ErrorMessageResourceType传递给LoadRegex,就像这个私有静态字符串LoadRegex(类型resourceType,字符串键),那么您可以使用var resourceManager=new resourceManager(resourceType),而不必硬编码资源
[LocalizedRegexAttribute]
public string PostalCode { get; set; }
 [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class LocalizedRegexAttribute : RegularExpressionAttribute
    {


        static LocalizedRegexAttribute()
        {
            // necessary to enable client side validation
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalizedRegexAttribute), typeof(RegularExpressionAttributeAdapter));
        }

        public LocalizedRegexAttribute(string _RegularExpression, string _ErrorMessageResourceName, Type _ErrorMessageResourceType)
            : base(LoadRegex(_RegularExpression))
        {
            ErrorMessageResourceType = _ErrorMessageResourceType;
            ErrorMessageResourceName = _ErrorMessageResourceName;

        }

        private static string LoadRegex(string key)
        {
            var resourceManager = new ResourceManager(typeof(Water.Localization.Resources.Global));
            return resourceManager.GetString(key);
        }
[LocalizedRegex("regExpValforPostal_ValidationExpression", "regExpValforPostal_ErrorMessage", typeof(Global))]
        public string PostalCode { get; set; }