forked from BornToBeRoot/NETworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerValidator.cs
More file actions
38 lines (29 loc) · 1.38 KB
/
Copy pathServerValidator.cs
File metadata and controls
38 lines (29 loc) · 1.38 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
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using NETworkManager.Localization.Resources;
using NETworkManager.Utilities;
namespace NETworkManager.Validators;
public class ServerValidator : ValidationRule
{
public ServerDependencyObjectWrapper Wrapper { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var allowOnlyIPAddress = Wrapper.AllowOnlyIPAddress;
var genericErrorResult =
allowOnlyIPAddress ? Strings.EnterValidIPAddress : Strings.EnterValidHostnameOrIPAddress;
var input = (value as string)?.Trim();
if (string.IsNullOrEmpty(input))
return new ValidationResult(false, genericErrorResult);
// Check if it is a valid IPv4 address like 192.168.0.1
if (Regex.IsMatch(input, RegexHelper.IPv4AddressRegex))
return ValidationResult.ValidResult;
// Check if it is a valid IPv6 address like ::1
if (Regex.IsMatch(input, RegexHelper.IPv6AddressRegex))
return ValidationResult.ValidResult;
// Check if it is a valid hostname like server-01 or server-01.example.com
if (Regex.IsMatch(input, RegexHelper.HostnameOrDomainRegex) && !allowOnlyIPAddress)
return ValidationResult.ValidResult;
return new ValidationResult(false, genericErrorResult);
}
}