Initial commit - Pingerino network monitoring application
هذا الالتزام موجود في:
90
Utilities/ValidationHelper.cs
Normal file
90
Utilities/ValidationHelper.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Pingerino.Utilities
|
||||
{
|
||||
public static class ValidationHelper
|
||||
{
|
||||
private static readonly Regex IpAddressRegex = new Regex(
|
||||
@"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$",
|
||||
RegexOptions.Compiled);
|
||||
|
||||
private static readonly Regex HostnameRegex = new Regex(
|
||||
@"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$",
|
||||
RegexOptions.Compiled);
|
||||
|
||||
public static bool IsValidIpAddress(string ipAddress)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ipAddress))
|
||||
return false;
|
||||
|
||||
return IPAddress.TryParse(ipAddress, out _) || IpAddressRegex.IsMatch(ipAddress);
|
||||
}
|
||||
|
||||
public static bool IsValidHostname(string hostname)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(hostname))
|
||||
return false;
|
||||
|
||||
if (hostname.Length > 253)
|
||||
return false;
|
||||
|
||||
return HostnameRegex.IsMatch(hostname);
|
||||
}
|
||||
|
||||
public static bool IsValidIpAddressOrHostname(string address)
|
||||
{
|
||||
return IsValidIpAddress(address) || IsValidHostname(address);
|
||||
}
|
||||
|
||||
public static bool IsValidPingInterval(string intervalText, out int interval)
|
||||
{
|
||||
interval = 0;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(intervalText))
|
||||
return false;
|
||||
|
||||
if (!int.TryParse(intervalText, out interval))
|
||||
return false;
|
||||
|
||||
return interval >= 100 && interval <= 60000; // 100ms to 60 seconds
|
||||
}
|
||||
|
||||
public static bool IsValidPingInterval(int interval)
|
||||
{
|
||||
return interval >= 100 && interval <= 60000;
|
||||
}
|
||||
|
||||
public static string SanitizeInput(string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
return string.Empty;
|
||||
|
||||
return input.Trim();
|
||||
}
|
||||
|
||||
public static string GetValidationErrorMessage(string fieldName, string value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
return $"{fieldName} cannot be empty.";
|
||||
|
||||
switch (fieldName.ToLower())
|
||||
{
|
||||
case "ip address":
|
||||
case "ipaddress":
|
||||
if (!IsValidIpAddressOrHostname(value))
|
||||
return "Please enter a valid IP address or hostname.";
|
||||
break;
|
||||
|
||||
case "interval":
|
||||
case "ping interval":
|
||||
if (!IsValidPingInterval(value, out _))
|
||||
return "Ping interval must be between 100 and 60000 milliseconds.";
|
||||
break;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
المرجع في مشكلة جديدة
حظر مستخدم