-
Notifications
You must be signed in to change notification settings - Fork 856
Expand file tree
/
Copy pathPortRangeValidator.cs
More file actions
49 lines (43 loc) · 1.53 KB
/
Copy pathPortRangeValidator.cs
File metadata and controls
49 lines (43 loc) · 1.53 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
43
44
45
46
47
48
49
using System.Globalization;
using System.Windows.Controls;
using NETworkManager.Localization.Resources;
namespace NETworkManager.Validators;
public class PortRangeValidator : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var isValid = true;
if (value == null)
return new ValidationResult(false, Strings.EnterValidPortOrPortRange);
foreach (var portOrRange in ((string)value).Replace(" ", "").Split(';'))
if (portOrRange.Contains('-'))
{
var portRange = portOrRange.Split('-');
if (int.TryParse(portRange[0], out var startPort) && int.TryParse(portRange[1], out var endPort))
{
if (!(startPort > 0 && startPort < 65536 && endPort > 0 && endPort < 65536 &&
startPort < endPort))
isValid = false;
}
else
{
isValid = false;
}
}
else
{
if (int.TryParse(portOrRange, out var portNumber))
{
if (!(portNumber > 0 && portNumber < 65536))
isValid = false;
}
else
{
isValid = false;
}
}
return isValid
? ValidationResult.ValidResult
: new ValidationResult(false, Strings.EnterValidPortOrPortRange);
}
}