154 lines
5.5 KiB
C#
154 lines
5.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Globalization;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Controls;
|
||
|
||
namespace JiangsuEarthquake.Common
|
||
{
|
||
public class CustomValidationRule : ValidationRule
|
||
{
|
||
public string ValidateType { get; set; }
|
||
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
|
||
{
|
||
try
|
||
{
|
||
string valueString = value?.ToString().Trim();
|
||
if (ValidateType == "Name")//姓名
|
||
{
|
||
string name = valueString;
|
||
if (!string.IsNullOrEmpty(name))
|
||
{
|
||
string errorMessage = string.Empty;
|
||
|
||
return name.Length > 10 ? new ValidationResult(false, "名称过长,名称长度不能超过10") : new ValidationResult(true, null);
|
||
}
|
||
}
|
||
else if (ValidateType == "Age")//年龄
|
||
{
|
||
string ageString = valueString;
|
||
if (!string.IsNullOrEmpty(ageString))
|
||
{
|
||
if (int.TryParse(ageString, out int age))
|
||
{
|
||
if (age is < 10 or > 150)
|
||
{
|
||
return new ValidationResult(false, "年龄不能小于10岁,也不能大于150岁");
|
||
}
|
||
}
|
||
|
||
return new ValidationResult(true, null);
|
||
}
|
||
}
|
||
else if (ValidateType == "Num")//数字
|
||
{
|
||
if (!string.IsNullOrEmpty(valueString))
|
||
{
|
||
if (!Regex.IsMatch(valueString, @"^\d+$"))
|
||
{
|
||
return new ValidationResult(false, "请输入正确的数字");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return new ValidationResult(false, "不能为空!!!");
|
||
}
|
||
|
||
}
|
||
else if (ValidateType == "IP")//IP
|
||
{
|
||
if (!string.IsNullOrEmpty(valueString))
|
||
{
|
||
IPAddress IP;
|
||
if (!IPAddress.TryParse(valueString, out IP))
|
||
{
|
||
return new ValidationResult(false, "IP格式不正确");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return new ValidationResult(false, "不能为空!!!");
|
||
}
|
||
}
|
||
else if (ValidateType == "Port")
|
||
{
|
||
if (!string.IsNullOrEmpty(valueString))
|
||
{
|
||
if (int.TryParse(valueString, out int port))
|
||
{
|
||
if (port is < 0 or > 65536)
|
||
{
|
||
return new ValidationResult(false, "请输入1~65535之间正确的端口号!");
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return new ValidationResult(false, "端口号不能为空!!!");
|
||
}
|
||
}
|
||
else if (ValidateType == "BDCycle") //北斗周期
|
||
{
|
||
if (!string.IsNullOrEmpty(valueString))
|
||
{
|
||
if (int.TryParse(valueString, out int port))
|
||
{
|
||
if (!Regex.IsMatch(valueString, @"^\d+$"))
|
||
{
|
||
return new ValidationResult(false, "请输入大于0小于24的整数");
|
||
}
|
||
|
||
if (port > 24 || port <= 0)
|
||
{
|
||
return new ValidationResult(false, "请输入大于0小于24的整数");
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return new ValidationResult(false, "北斗周期不能为空!!!");
|
||
}
|
||
}
|
||
else if (ValidateType == "AirSpeedSet") //风速
|
||
{
|
||
if (!string.IsNullOrEmpty(valueString))
|
||
{
|
||
if (int.TryParse(valueString, out int port))
|
||
{
|
||
if (port < 10)
|
||
{
|
||
return new ValidationResult(false, "风速应大于10米每秒");
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return new ValidationResult(false, "风速不能为空!!!");
|
||
}
|
||
}
|
||
else if (ValidateType == "Attitude_angle_y")
|
||
{
|
||
|
||
}
|
||
else if (ValidateType == "Attitude_angle_x")
|
||
{
|
||
|
||
}
|
||
else if (ValidateType == "Attitude_angle_z")
|
||
{
|
||
|
||
}
|
||
return new ValidationResult(true, null);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
return new ValidationResult(false, e.Message);
|
||
}
|
||
}
|
||
}
|
||
}
|