C# 忽略MVC模型中的正则表达式,或根据用户输入选择另一个正则表达式

C# 忽略MVC模型中的正则表达式,或根据用户输入选择另一个正则表达式,c#,asp.net-mvc,C#,Asp.net Mvc,在我的一个类中有两个字段作为地址 public string Country { get; set; } [Required(ErrorMessage = "Postcode is required")] [RegularExpression(@"REGEX", ErrorMessage = "Please enter a valid UK Postcode)] public string postcode { get; set;} 但是,如果用户选择英国以外的国家,则我希望我的邮政编码字段

在我的一个类中有两个字段作为地址

public string Country { get; set; }
[Required(ErrorMessage = "Postcode is required")]
[RegularExpression(@"REGEX", 
ErrorMessage = "Please enter a valid UK Postcode)]
public string postcode { get; set;}

但是,如果用户选择英国以外的国家,则我希望我的邮政编码字段至少忽略REGEX,并且在理想情况下,根据国家使用另一个REGEX进行验证。有人能建议这在模型本身中是否可行吗

有几个不同的选项供您选择:

  • 创建一个100%自定义验证属性,将
    Required
    RegularExpression
    属性组合到您的需要中。因此,在该自定义属性中,您将执行所需的所有验证,并将该值与
    Country
    属性进行比较,以便根据需要选择性地应用正则表达式

  • 根据您关心的国家/地区创建不同的
    postcode
    属性,并使用类似“RequiredIfAttribute”(请参阅)的内容来确定实际需要哪个属性。然后可以使用Javascript显示/隐藏适当的输入字段


  • 有几个不同的选项供您选择:

  • 创建一个100%自定义验证属性,将
    Required
    RegularExpression
    属性组合到您的需要中。因此,在该自定义属性中,您将执行所需的所有验证,并将该值与
    Country
    属性进行比较,以便根据需要选择性地应用正则表达式

  • 根据您关心的国家/地区创建不同的
    postcode
    属性,并使用类似“RequiredIfAttribute”(请参阅)的内容来确定实际需要哪个属性。然后可以使用Javascript显示/隐藏适当的输入字段


  • 您可以使用
    IValidatableObject
    执行此操作:

    class MyClass : IValidatableObject {
    
       public string Country { get; set; }
    
       [Required(ErrorMessage = "Postcode is required")]
       public string postcode { get; set;}
    
       public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
    
          if (!String.IsNullOrEmpty(Country) 
             && !String.IsNullOrEmpty(postcode)) {
    
             switch (Country.ToUpperInvariant()) { 
                case "UK":
                   if (!Regex.IsMatch(postcode, "[regex]"))
                      yield return new ValidationResult("Invalid UK postcode.", new[] { "postcode" });
                   break;
    
                default:
                   break;
             }
          }
       }
    }
    
    class MyClass:IValidatableObject{
    公共字符串国家{get;set;}
    [必需(ErrorMessage=“需要邮政编码”)]
    公共字符串邮政编码{get;set;}
    公共IEnumerable验证(ValidationContext ValidationContext){
    如果(!String.IsNullOrEmpty(国家/地区)
    &&!String.IsNullOrEmpty(邮政编码)){
    开关(Country.toupper不变量()){
    案例“英国”:
    如果(!Regex.IsMatch(邮政编码,“[Regex]”)
    返回新的ValidationResult(“英国邮政编码无效”,新[]{“邮政编码”});
    打破
    违约:
    打破
    }
    }
    }
    }
    
    您可以使用
    IValidatableObject
    执行此操作:

    class MyClass : IValidatableObject {
    
       public string Country { get; set; }
    
       [Required(ErrorMessage = "Postcode is required")]
       public string postcode { get; set;}
    
       public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
    
          if (!String.IsNullOrEmpty(Country) 
             && !String.IsNullOrEmpty(postcode)) {
    
             switch (Country.ToUpperInvariant()) { 
                case "UK":
                   if (!Regex.IsMatch(postcode, "[regex]"))
                      yield return new ValidationResult("Invalid UK postcode.", new[] { "postcode" });
                   break;
    
                default:
                   break;
             }
          }
       }
    }
    
    class MyClass:IValidatableObject{
    公共字符串国家{get;set;}
    [必需(ErrorMessage=“需要邮政编码”)]
    公共字符串邮政编码{get;set;}
    公共IEnumerable验证(ValidationContext ValidationContext){
    如果(!String.IsNullOrEmpty(国家/地区)
    &&!String.IsNullOrEmpty(邮政编码)){
    开关(Country.toupper不变量()){
    案例“英国”:
    如果(!Regex.IsMatch(邮政编码,“[Regex]”)
    返回新的ValidationResult(“英国邮政编码无效”,新[]{“邮政编码”});
    打破
    违约:
    打破
    }
    }
    }
    }
    
    谢谢Matt我用了你的答案选项。谢谢Matt我用了你的答案选项。