390 lines
12 KiB
C#
390 lines
12 KiB
C#
using System.Text.RegularExpressions;
|
||||
|
|
|
|||
|
|
namespace JSMachine.WMS.Common.Helper
|
|||
|
|
{
|
|||
|
|
public static class ValidatorHelper
|
|||
|
|
{
|
|||
|
|
//private static Regex RegNumber = new Regex("^[0-9]+$");//正整数
|
|||
|
|
|
|||
|
|
//private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");//正负整数
|
|||
|
|
|
|||
|
|
//private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");//小数
|
|||
|
|
|
|||
|
|
//private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
|
|||
|
|
|
|||
|
|
//private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
|
|||
|
|
|
|||
|
|
//private static Regex RegChinese = new Regex("[\u4e00-\u9fa5]");//中文
|
|||
|
|
|
|||
|
|
//private static Regex RegUrl = new Regex("^http://([w-]+.)+[w-]+(/[w-./ %&=]*)$");//带http://的网址
|
|||
|
|
|
|||
|
|
//private static Regex RegTel = new Regex("^(\d{3}-\d{8})$|^(\d{4}-\d{7})$|^(\d{11})$");//国内电话号码 ,正确格式为:“XXXX-XXXXXXX”,“XXXX-XXXXXXXX”,“XXX-XXXXXXX”, “XXXXXXXXXXX”。
|
|||
|
|
|
|||
|
|
//private static Regex RegQQ = new Regex("^[1-9]*[1-9][0-9]*$");//匹配腾讯QQ号
|
|||
|
|
|
|||
|
|
//private static Regex RegIDCard = new Regex(@"(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$)|(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)");//匹配国内身份证号〕
|
|||
|
|
|
|||
|
|
//private static Regex RegUserName = new Regex(@"^[a-zA-Z]\w{5,15}$");//匹配由字母开头,数字、26个英文字母或者下划线组成6-16位的字符串
|
|||
|
|
|
|||
|
|
//private static Regex RegEnglish = new Regex("^[A-Za-z]+$");//由26个英文字母组成的字符串
|
|||
|
|
|
|||
|
|
//private static Regex RegTrim = new Regex(@"(^\s*)|(\s*$)");//首尾空格的行
|
|||
|
|
|
|||
|
|
//private static Regex RegTrimRow = new Regex(@"\n[\s| ]*\r");//空行
|
|||
|
|
|
|||
|
|
//private static Regex RegMobile = new Regex(@"^((\(\d{2,3}\))|(\d{3}\-))?1[3|5]\d{9}$");//国内手机
|
|||
|
|
|
|||
|
|
#region 对入库字符进行编码和转换。
|
|||
|
|
/// <summary>
|
|||
|
|
/// 对入库字符进行编码和转换
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="str"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string EncodeStr(string str)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
str = str.Replace("'", "’");
|
|||
|
|
|
|||
|
|
str = str.Replace("\"", """);
|
|||
|
|
|
|||
|
|
str = str.Replace("<", "<");
|
|||
|
|
|
|||
|
|
str = str.Replace(">", ">");
|
|||
|
|
|
|||
|
|
str = str.Replace("\n", "<br>");
|
|||
|
|
|
|||
|
|
return str;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 对出库字符进入显示时的转换。
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 对出库字符进入显示时的转换
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="str"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string DecodeStr(string str)
|
|||
|
|
{
|
|||
|
|
str = str.Replace("’", "'");
|
|||
|
|
|
|||
|
|
str = str.Replace(""", "\"");
|
|||
|
|
|
|||
|
|
str = str.Replace("<", "<");
|
|||
|
|
|
|||
|
|
str = str.Replace(">", ">");
|
|||
|
|
|
|||
|
|
str = str.Replace("<br>", "\n");
|
|||
|
|
|
|||
|
|
return str;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 数字字符串检查
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否数字字符串
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData">输入字符串</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsNumber(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegNumber = new Regex(@"^\+?[1-9][0-9]*$");//正整数
|
|||
|
|
|
|||
|
|
Match m = RegNumber.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否数字字符串 可带正负号
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData">输入字符串</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsNumberSign(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");//正负整数
|
|||
|
|
|
|||
|
|
Match m = RegNumberSign.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否是浮点数
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData">输入字符串</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsDecimal(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");//小数
|
|||
|
|
|
|||
|
|
Match m = RegDecimal.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否是浮点数 可带正负号
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData">输入字符串</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsDecimalSign(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
|
|||
|
|
|
|||
|
|
Match m = RegDecimalSign.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 正小数
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsReDecimal(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegDecimal = new Regex("^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$");//小数
|
|||
|
|
|
|||
|
|
Match m = RegDecimal.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 中文检测
|
|||
|
|
/// <summary>
|
|||
|
|
/// 检测是否有中文字符
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsChinese(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegChinese = new Regex("^[\u4e00-\u9fa5]+$");//中文
|
|||
|
|
|
|||
|
|
Match m = RegChinese.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 邮件地址
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否是邮件地址
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData">输入字符串</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsEmail(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info|cn)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
|
|||
|
|
|
|||
|
|
Match m = RegEmail.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region URL网址
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否是带http://的网址
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsUrl(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegUrl = new Regex("^http://([w-]+.)+[w-]+(/[w-./ %&=]*)$");//带http://的网址
|
|||
|
|
|
|||
|
|
Match m = RegUrl.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 固定电话
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否是国内电话号码
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsTel(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegTel = new Regex(@"^(\d{3}-\d{8})$|^(\d{4}-\d{7})$|^(\d{11})$");//国内电话号码 ,正确格式为:“XXXX-XXXXXXX”,“XXXX-XXXXXXXX”,“XXX-XXXXXXX”, “XXX-XXXXXXXX”,“XXXXXXX”,“XXXXXXXX”。
|
|||
|
|
|
|||
|
|
Match m = RegTel.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region QQ
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否是QQ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsQQ(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegQQ = new Regex("^[1-9]*[1-9][0-9]*$");//匹配腾讯QQ号
|
|||
|
|
|
|||
|
|
Match m = RegQQ.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 身份证
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否是国内身份证号
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsIDCard(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegIDCard = new Regex(@"(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$)|(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)");//匹配国内身份证号〕
|
|||
|
|
|
|||
|
|
Match m = RegIDCard.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 账号
|
|||
|
|
/// <summary>
|
|||
|
|
/// 账号是否合法,字母开头,数字、26个英文字母或者下划线组成6-16位的字符串
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsUserName(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegUserName = new Regex(@"^[a-zA-Z]\w{5,15}$");//匹配由字母开头,数字、26个英文字母或者下划线组成6-16位的字符串
|
|||
|
|
|
|||
|
|
Match m = RegUserName.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 英文字母
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否是由26个英文字母组成的字符串
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsEnglish(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegEnglish = new Regex("^[A-Za-z]+$");//由26个英文字母组成的字符串
|
|||
|
|
|
|||
|
|
Match m = RegEnglish.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 空行
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否有空行
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsTrimRow(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegTrimRow = new Regex(@"\n[\s| ]*\r");//空行
|
|||
|
|
|
|||
|
|
Match m = RegTrimRow.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 手机
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否是国内手机
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsMobile(string inputData)
|
|||
|
|
{
|
|||
|
|
Regex RegMobile = new Regex(@"^((\(\d{2,3}\))|(\d{3}\-))?1[3|5]\d{9}$");//国内手机
|
|||
|
|
|
|||
|
|
Match m = RegMobile.Match(inputData);
|
|||
|
|
|
|||
|
|
return m.Success;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 日期
|
|||
|
|
/// <summary>
|
|||
|
|
/// 检查是否是日期
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsDate(string inputData)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
DateTime.Parse(inputData);
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 检查字符串最大长度,返回指定长度的串
|
|||
|
|
/// <summary>
|
|||
|
|
/// 检查字符串最大长度,返回指定长度的串
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="sqlInput">输入字符串</param>
|
|||
|
|
/// <param name="maxLength">最大长度</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string SqlText(string sqlInput, int maxLength)
|
|||
|
|
{
|
|||
|
|
if (sqlInput != null && sqlInput != string.Empty)
|
|||
|
|
{
|
|||
|
|
sqlInput = sqlInput.Trim();
|
|||
|
|
|
|||
|
|
if (sqlInput.Length > maxLength)//按最大长度截取字符串
|
|||
|
|
|
|||
|
|
sqlInput = sqlInput.Substring(0, maxLength);
|
|||
|
|
}
|
|||
|
|
return sqlInput;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 空字符串
|
|||
|
|
/// <summary>
|
|||
|
|
/// 空字符串
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="inputData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsEmptyStr(string inputData)
|
|||
|
|
{
|
|||
|
|
var result = false;
|
|||
|
|
if (!string.IsNullOrEmpty(inputData))
|
|||
|
|
{
|
|||
|
|
result = true;
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
}
|