-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPAddressValidationRule.cs
More file actions
42 lines (41 loc) · 1.21 KB
/
IPAddressValidationRule.cs
File metadata and controls
42 lines (41 loc) · 1.21 KB
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
42
using System.Globalization;
using System.Linq;
using System.Net;
using System.Windows.Controls;
namespace NetSpector
{
/// <summary>
/// 自定义规则:IP地址验证检查
/// </summary>
public class IPAddressValidationRule : ValidationRule
{
/// <summary>
/// 构造函数
/// </summary>
public IPAddressValidationRule()
{
}
/// <summary>
/// 执行验证检查
/// </summary>
/// <param name="value">输入值</param>
/// <param name="cultureInfo">区域信息</param>
/// <returns>检查结果</returns>
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string text = value?.ToString().Trim();
if (text == null || text.Length == 0)
{
return ValidationResult.ValidResult;
}
else if (text.Split(',').Count(item => !IPAddress.TryParse(item, out IPAddress unused)) > 0)
{
return new ValidationResult(false, "字符输入不正确");
}
else
{
return ValidationResult.ValidResult;
}
}
}
}