using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Controls; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar; namespace XHandler.View.Consumables { public class ConsumablesValidationRule : ValidationRule { public string ValidateType { get; set; } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { try { /* 1.不为空 2.正数 3.整数 4.小数点后位数 4@[2] 5.范围 5@[0-10000] 6.长度 6@[1000] */ string valueString = value?.ToString().Trim(); string message; foreach (var item in ValidateType.Split(',')) { switch (item) { case "1": if (!IsNullOrEmpty(valueString, out message)) { return new ValidationResult(false, message); } break; case "2": if (!IsPositiveNumber(valueString, out message)) { return new ValidationResult(false, message); } break; case "3": if (!IsIntegerr(valueString, out message)) { return new ValidationResult(false, message); } break; default: if (item.Contains("4@")) { if (valueString.Contains('.')) { //获取小数点后位数 int lenth = Convert.ToInt32(item.Substring(item.IndexOf('[') + 1, item.Length - 4)); if (valueString.Substring(valueString.IndexOf('.') + 1).Length > lenth) { return new ValidationResult(false, $"小数点后可输入[{lenth}]位"); } } } else if (item.Contains("5@")) { string rand = string.Empty; if (item.Contains("(")) { rand = item.Substring(item.IndexOf('(') + 1, item.Length - 4); } else { //获取范围数组 rand = item.Substring(item.IndexOf('[') + 1, item.Length - 4); } string[] arr = rand.Split('-'); if (item.Contains("(")&& item.Contains(")")) { if (!IsRangeSection(valueString, Convert.ToDouble(arr[0]), Convert.ToDouble(arr[1]), false,2, out message)) { return new ValidationResult(false, message); } } else if (item.Contains("(") && !item.Contains(")")) { if (!IsRangeSection(valueString, Convert.ToDouble(arr[0]), Convert.ToDouble(arr[1]), false,1, out message)) { return new ValidationResult(false, message); } } else if (!item.Contains("(") && item.Contains(")")) { if (!IsRangeSection(valueString, Convert.ToDouble(arr[0]), Convert.ToDouble(arr[1]), false,3, out message)) { return new ValidationResult(false, message); } } else { if (!IsRange(valueString, Convert.ToDouble(arr[0]), Convert.ToDouble(arr[1]), false, out message)) { return new ValidationResult(false, message); } } //if (double.TryParse(valueString, out double num)) //{ // //获取范围数组 // string rand = item.Substring(item.IndexOf('[') + 1, item.Length - 4); // string[] arr = rand.Split('-'); // if (Convert.ToDouble(arr[0]) > num || num > Convert.ToDouble(arr[1])) // { // return new ValidationResult(false, $"请输入[{rand}]内的值"); // } //} //else //{ // return new ValidationResult(false, $"请输入正确的值"); //} } else if (item.Contains("6@")) { //获取范围数组 string rand = item.Substring(item.IndexOf('[') + 1, item.Length - 4); if (!IsLength(valueString, Convert.ToInt32(rand), out message)) { return new ValidationResult(false, message); } } else if (item.Contains("7@")) { string rand = string.Empty; if (item.Contains("(")) { rand = item.Substring(item.IndexOf('(') + 1, item.Length - 4); } else { //获取范围数组 rand = item.Substring(item.IndexOf('[') + 1, item.Length - 4); } string[] arr = rand.Split('~'); if (item.Contains("(") && item.Contains(")")) { if (!IsRangeSection(valueString, Convert.ToDouble(arr[0]), Convert.ToDouble(arr[1]), false, 2, out message)) { return new ValidationResult(false, message); } } else if (item.Contains("(") && !item.Contains(")")) { if (!IsRangeSection(valueString, Convert.ToDouble(arr[0]), Convert.ToDouble(arr[1]), false, 1, out message)) { return new ValidationResult(false, message); } } else if (!item.Contains("(") && item.Contains(")")) { if (!IsRangeSection(valueString, Convert.ToDouble(arr[0]), Convert.ToDouble(arr[1]), false, 3, out message)) { return new ValidationResult(false, message); } } else { if (!IsRange(valueString, Convert.ToDouble(arr[0]), Convert.ToDouble(arr[1]), false, out message)) { return new ValidationResult(false, message); } } //if (double.TryParse(valueString, out double num)) //{ // //获取范围数组 // string rand = item.Substring(item.IndexOf('[') + 1, item.Length - 4); // string[] arr = rand.Split('-'); // if (Convert.ToDouble(arr[0]) > num || num > Convert.ToDouble(arr[1])) // { // return new ValidationResult(false, $"请输入[{rand}]内的值"); // } //} //else //{ // return new ValidationResult(false, $"请输入正确的值"); //} } break; } } return new ValidationResult(true, null); } catch (Exception e) { return new ValidationResult(false, e.Message); } } /// /// 验证为空 /// /// 验证字符串 /// 返回消息 /// 验证结果 public static bool IsNullOrEmpty(string valueString, out string message) { message = ""; if (string.IsNullOrEmpty(valueString)) { message = "不能为空"; return false; } return true; } /// /// 验证正数 /// /// 验证字符串 /// 返回消息 /// 验证结果 public static bool IsPositiveNumber(string valueString, out string message) { message = ""; if (IsNullOrEmpty(valueString, out message)) { if (!Regex.IsMatch(valueString, "^[0-9]+(.[0-9]+)?$")) { message = "请输入正数"; return false; } } else { return false; } return true; } /// /// 验证整数 /// /// 验证字符串 /// 返回消息 /// 验证结果 public static bool IsIntegerr(string valueString, out string message) { message = ""; if (IsNullOrEmpty(valueString, out message)) { if (!Regex.IsMatch(valueString, "^([0-9]{1,})$")) { message = "请输入整数"; return false; } } else { return false; } return true; } /// /// 验证正整数 /// /// 验证字符串 /// 返回消息 /// 验证结果 public static bool IsPositiveIntegerr(string valueString, out string message) { message = ""; if (!Regex.IsMatch(valueString, "^[1-9]\\d*$")) { message = "请输入正整数"; return false; } return true; } /// /// 验证长度 /// /// 验证字符串 /// 长度 /// 返回消息 /// 验证结果 public static bool IsLength(string valueString, int length, out string message) { message = ""; if (valueString.Length > length) { message = $"输入字符个数应在[{length}]内"; return false; } return true; } /// /// 验证范围 /// /// 验证字符串 /// 开始 /// 结束 /// 是否整数 /// 返回消息 /// public static bool IsRange(string valueString, double start, double end, bool isPositive, out string message) { message = ""; if (isPositive) { if (!int.TryParse(valueString, out int value)) { message = $"请输入[{start}-{end}]内的整数"; return false; } } if (double.TryParse(valueString, out double num)) { if (start > num || num > end) { message = $"请输入[{start}-{end}]内的值"; return false; } } else { message = "请输入正确的值"; return false; } return true; } /// /// 验证范围 /// /// 验证字符串 /// 开始 /// 结束 /// 是否整数 /// 1;左开右闭;2:全开;3:左闭右开 /// 返回消息 /// public static bool IsRangeSection(string valueString, double start, double end, bool isPositive,int mark, out string message) { message = ""; if (isPositive) { if (!int.TryParse(valueString, out int value)) { message = $"请输入[{start}-{end}]内的整数"; return false; } } if (double.TryParse(valueString, out double num)) { if (mark == 1) { if (start >= num || num > end) { message = $"请输入[{start}-{end}]内的值"; return false; } } else if (mark == 2) { if (start >= num || num >= end) { message = $"请输入[{start}-{end}]内的值"; return false; } } else if (mark == 3) { if (start > num || num >= end) { message = $"请输入[{start}-{end}]内的值"; return false; } } } else { message = "请输入正确的值"; return false; } return true; } } }