using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.Drawing; using System.Text.RegularExpressions; using System.Windows.Forms; namespace ZJ_BYD.Untils { public static class RegexCheckHelper { /// /// 提供键值对的正则表达式 /// private static Dictionary rege; /// /// 定义校验数据的正则表达式,后期可添加 /// /// /// public static string Get(RegexEnum regexEnum) { rege = rege ?? new Dictionary { {RegexEnum.NotNull,@"^[\s\S]*.*[^\s][\s\S]*$"}, {RegexEnum.Phone,@"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$"}, {RegexEnum.IDCard, @"^([1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3})|([1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx]))$"}, {RegexEnum.Letter, @"^[A-Za-z]+$"}, {RegexEnum.Number, @"^[0-9]+$"}, {RegexEnum.LetterNumber, @"^[A-Za-z0-9]+$"}, {RegexEnum.ChineseChar, @"^[\u4e00-\u9fa5]+$"}, {RegexEnum.Email, @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"}, }; return rege[regexEnum]; } /// /// 设置错误信息的背景色 /// public static Color? ErrorColor { get; set; } /// /// 是否自定义错误提示信息,true时,调用 ShowMsg,false时使用系统默认提示 /// public static bool isCustomMsg = false; /// /// 提示信息处理方法,可以单独定义并修改提示方式 /// public static event EventHandler ShowMsg; private static void OnShowMsg(Control con, string msg) { var handler = ShowMsg; var jEvent = new JEventArgs() { msg = msg }; if (handler != null) handler(con, jEvent); } /// /// 数据校验 单个控件 /// /// 需要校验的控件 /// 需要校验的类型 /// 错误提示信息 /// 是否非空校验 /// public static bool RegexText(this Control con, RegexEnum regexEnum, string msg, bool isNotNull = false) { var isTrue = true; var oldColor = con.BackColor; con.Tag = regexEnum + "&" + msg + "&" + isNotNull; con.Leave += (sender, e) => { isTrue = TextRegex(con, regexEnum, msg, isNotNull); }; con.GotFocus += (sender, e) => { con.BackColor = oldColor; }; return isTrue; } /// /// 数据校验 一组控件 /// /// 扩展源 /// 需要校验的控件一组控件 /// 需要校验的类型 /// 错误提示信息 /// 是否非空校验 /// public static bool RegexText(this Form form, Control[] cons, RegexEnum[] regexEnum, string[] msg, bool[] isNotNull) { for (var i = 0; i < cons.Length; i++) { var con = cons[i]; var _msg = "输入非法"; try { _msg = msg[i]; } catch (Exception ex) { } var isTrue = con.RegexText(regexEnum[i], _msg, isNotNull[i]); if (isTrue == false) return false; } return true; } /// /// 保存操作时多控件数据校验 /// /// /// /// public static bool RegexSave(this XtraUserControl xtraUserControl, Control[] cons, out string msg) { msg = ""; foreach (var con in cons) { var valList = con.Tag.ToString().Split('&'); msg = valList[1]; var enumType = (RegexEnum)Enum.Parse(typeof(RegexEnum), valList[0]); var flg = TextRegex(con, enumType, valList[1], bool.Parse(valList[2])); if (flg == false) { con.BackColor = ErrorColor ?? Color.AntiqueWhite; return false; } } return true; } /// /// 保存操作时多控件数据校验 /// /// /// /// public static bool RegexSave(this XtraForm xtraForm, Control[] cons, out string msg) { msg = ""; foreach (var con in cons) { var valList = con.Tag.ToString().Split('&'); msg = valList[1]; var enumType = (RegexEnum)Enum.Parse(typeof(RegexEnum), valList[0]); var flg = TextRegex(con, enumType, valList[1], bool.Parse(valList[2])); if (flg == false) { con.BackColor = ErrorColor ?? Color.AntiqueWhite; return false; } } return true; } /// /// 显示处理 /// /// /// private static void ShowMsgBox(Control con, string msg) { if (isCustomMsg) { OnShowMsg(con, msg); } else { XtraMessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } /// /// 校验数据 /// /// 控件 /// 是否校验非空值 /// 错误提示消息 /// 校验类型 /// private static bool TextRegex(Control con, RegexEnum regexEnum, string msg, bool isNotNull = false) { var text = con.Text.Trim(); #region 非空校验 string _msg; if (isNotNull && text == "") { _msg = msg + "不能为空值!"; con.BackColor = ErrorColor ?? Color.AntiqueWhite; ShowMsgBox(con, _msg); return false; } #endregion #region 正则校验 var str = Get(regexEnum); var regex = new Regex(str); var isTrue = regex.IsMatch(text); if (isTrue) return true; _msg = msg + "格式错误!"; ShowMsgBox(con, _msg); con.BackColor = ErrorColor ?? Color.AntiqueWhite; return false; #endregion } } public enum RegexEnum { /// /// 电话号 /// Phone, /// /// 字母 /// Letter, /// /// 数字 /// Number, /// /// 字母和数字 /// LetterNumber, /// /// 身份证号 /// IDCard, /// /// 电子邮件 /// Email, /// /// 汉字 /// ChineseChar, /// /// 非空 /// NotNull, } public class JEventArgs : EventArgs { /// /// 存储错误信息 /// public string msg; } }