You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
8.2 KiB
C#

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
{
/// <summary>
/// 提供键值对的正则表达式
/// </summary>
private static Dictionary<RegexEnum, string> rege;
/// <summary>
/// 定义校验数据的正则表达式,后期可添加
/// </summary>
/// <param name="regexEnum"></param>
/// <returns></returns>
public static string Get(RegexEnum regexEnum)
{
rege = rege ?? new Dictionary<RegexEnum, string>
{
{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];
}
/// <summary>
/// 设置错误信息的背景色
/// </summary>
public static Color? ErrorColor { get; set; }
/// <summary>
/// 是否自定义错误提示信息true时调用 ShowMsg,false时使用系统默认提示
/// </summary>
public static bool isCustomMsg = false;
/// <summary>
/// 提示信息处理方法,可以单独定义并修改提示方式
/// </summary>
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);
}
/// <summary>
/// 数据校验 单个控件
/// </summary>
/// <param name="con">需要校验的控件</param>
/// <param name="regexEnum">需要校验的类型</param>
/// <param name="msg">错误提示信息</param>
/// <param name="isNotNull">是否非空校验</param>
/// <returns></returns>
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;
}
/// <summary>
/// 数据校验 一组控件
/// </summary>
/// <param name="form">扩展源</param>
/// <param name="cons">需要校验的控件一组控件</param>
/// <param name="regexEnum">需要校验的类型</param>
/// <param name="msg">错误提示信息</param>
/// <param name="isNotNull">是否非空校验</param>
/// <returns></returns>
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;
}
/// <summary>
/// 保存操作时多控件数据校验
/// </summary>
/// <param name="form"></param>
/// <param name="cons"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 保存操作时多控件数据校验
/// </summary>
/// <param name="form"></param>
/// <param name="cons"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 显示处理
/// </summary>
/// <param name="con"></param>
/// <param name="msg"></param>
private static void ShowMsgBox(Control con, string msg)
{
if (isCustomMsg)
{
OnShowMsg(con, msg);
}
else
{
XtraMessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
/// <summary>
/// 校验数据
/// </summary>
/// <param name="con">控件</param>
/// <param name="isNotNull">是否校验非空值</param>
/// <param name="msg">错误提示消息</param>
/// <param name="regexEnum">校验类型</param>
/// <returns></returns>
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
{
/// <summary>
/// 电话号
/// </summary>
Phone,
/// <summary>
/// 字母
/// </summary>
Letter,
/// <summary>
/// 数字
/// </summary>
Number,
/// <summary>
/// 字母和数字
/// </summary>
LetterNumber,
/// <summary>
/// 身份证号
/// </summary>
IDCard,
/// <summary>
/// 电子邮件
/// </summary>
Email,
/// <summary>
/// 汉字
/// </summary>
ChineseChar,
/// <summary>
/// 非空
/// </summary>
NotNull,
}
public class JEventArgs : EventArgs
{
/// <summary>
/// 存储错误信息
/// </summary>
public string msg;
}
}