20240301_JSEQ_upperpc/JiangsuEarthquake_test/JiangsuEarthquake/Common/CustomValidationRule.cs
XuMin 748090f317 修改部分:
1 地震仪告警记录中故障次数隐藏;电力载波机和光电交换机的供电状态隐藏;
2 除漏水和保护板事件的数据按照浮点数解析,别的按照整数解析;
3 解决TCP Server和Client存在的问题,包括无法监测到客户端连接,无法监测到服务端断开等问题;
2024-08-13 14:35:33 +08:00

182 lines
6.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 == "UserName")
{
if (!string.IsNullOrEmpty(valueString))
{
if(Regex.IsMatch(valueString, @"[\u4e00-\u9fff]"))
{
return new ValidationResult(false, "名称不能含中文!");
}
}
else
{
return new ValidationResult(false, "名称不能为空!!!");
}
}
else if (ValidateType == "Password")
{
if (!string.IsNullOrEmpty(valueString))
{
if (Regex.IsMatch(valueString, @"[\u4e00-\u9fff]"))
{
return new ValidationResult(false, "密码不能含中文!");
}
}
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);
}
}
}
}