我正在使用数据注释来检测网页文本框中的非法字符。
[RegularExpression(Constants.LegalName, ErrorMessage = "Full name is invalid.")]
public string FullName {
get;
set;
}
const string LegalName= @"^[a-zA-Z '-.]*$";我使用以下代码验证这些字段
Validator.TryValidateObject(
inputFieldValue,
new ValidationContext(inputFieldValue, null, null),
result,
true);如果检测到任何非法字符,则结果将有一个错误字符串"Full name is invalid“。
如何获取在字段中键入的非法字符列表?字符串inputFieldValue将包含用户在字段中键入的内容。如何获取带有@"^[a-zA-Z '-.]*$";等reg express的所有非法字符的列表
谢谢。
发布于 2017-01-20 02:52:07
我不确定你能不能用TryValidateObject得到它。你必须分别找到它们:
const string ValidCharPattern = @"[a-zA-Z '-.]";
const string LegalName= @"^" + ValidCharPattern + @"*$";
var invalidChars = Regex
.Replace(
input: inputFieldValue,
pattern: ValidCharPattern,
replacement: String.Empty)
.Distinct();https://stackoverflow.com/questions/41749430
复制相似问题