-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegexValidationRule.cs
41 lines (40 loc) · 1.14 KB
/
RegexValidationRule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Controls;
namespace NetSpector
{
/// <summary>
/// 自定义规则:正则表达式验证检查
/// </summary>
public class RegexValidationRule : ValidationRule
{
/// <summary>
/// 正则表达式模板
/// </summary>
public string Pattern { get; set; }
/// <summary>
/// 构造函数
/// </summary>
public RegexValidationRule()
{
}
/// <summary>
/// 执行验证检查
/// </summary>
/// <param name="value">输入值</param>
/// <param name="cultureInfo">区域信息</param>
/// <returns>检查结果</returns>
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
Regex regex = new Regex(Pattern);
if (regex.IsMatch((string)value))
{
return ValidationResult.ValidResult;
}
else
{
return new ValidationResult(false, "字符输入不正确");
}
}
}
}