protected override void Validate(DomainObject domainObject, DomainProperty property, string propertyValue, ValidationResult result) { if (string.IsNullOrEmpty(propertyValue)) { return; } if (!string.IsNullOrEmpty(_pattern)) { if (!_regex.IsMatch(propertyValue)) { result.AddError(property.Name, StringFormatError, string.Format(Extensions.Strings.DoesNotMeetRule, property.Name, _pattern)); } } else { var value = propertyValue; if (_contains.Length > 0) { foreach (string temp in _contains) { value = value.Replace(temp, string.Empty); } } RegexPool reg = null; string message = null; if (IsChineseAndLetterAndNumber(_formatValue)) { reg = _chineseAndLetterAndNumberRegex; message = Extensions.Strings.CEN; } else if (IsLetterAndNumber(_formatValue)) { reg = _letterAndNumberRegex; message = Extensions.Strings.EN; } else if (IsChineseAndLetter(_formatValue)) { reg = _chineseAndLetterRegex; message = Extensions.Strings.CE; } else if (IsChineseAndNumber(_formatValue)) { reg = _chineseAndNumberRegex; message = Extensions.Strings.CN; } if (reg != null && !reg.IsMatch(value)) { result.AddError(property.Name, StringFormatError, string.Format(Extensions.Strings.CanOnlyInclude, property.Name, message)); } } }
public StringFormatValidator(string pattern) { _pattern = pattern; _regex = new RegexPool(pattern, RegexOptions.IgnoreCase); }