I trying to validate string attribute in an object that created from data annotated model. This validation logic contains regular expression that checks whether the string is not empty or blank. Validation only fails when stringing with multiple spaces ("_________") it does not fail when attribute without space, that mean empty string("").
Annotated data model,
class Company
{
[RegularExpression(@".*\S+.*$", ErrorMessage = "Website is empty")]
[JsonProperty(PropertyName = "website")]
public string Website { get; set; }
}
Calling for validation,
Company company = new Company(){
Website = ""
};
var validationResults = new List<ValidationResult>();
var context = new ValidationContext(company, serviceProvider: null, items: null);
Validator.TryValidateObject(company, context, validationResults, true);
validationResults not capture the validation error.