You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
267 lines
8.0 KiB
267 lines
8.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace QM.Common
|
|
{
|
|
///<summary>
|
|
/// 模块编号:QM.Common.SysValidate
|
|
/// 作 用:共通校验方法
|
|
/// 作 者:
|
|
/// 编写日期:2014-05-05
|
|
///</summary>
|
|
public class SysValidate
|
|
{
|
|
|
|
#region 变量
|
|
|
|
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 RegCHZN = new Regex("[\u4e00-\u9fa5]");
|
|
|
|
private static Regex RegIP = new Regex("^((25[0-5]|2[0-4]\\d|1?\\d{1,2})\\.){3}(25[0-5]|2[0-4]\\d|1?\\d{1,2})$ ");
|
|
#endregion
|
|
|
|
#region 数值校验
|
|
/// <summary>
|
|
/// 判断整型数字
|
|
/// </summary>
|
|
/// <param name="pIntValue">需要校验的数值</param>
|
|
/// <returns>
|
|
/// 参数为数字类型返回ture,否则返回false。
|
|
/// </returns>
|
|
public static bool IsInt(string pIntValue)
|
|
{
|
|
#region
|
|
try
|
|
{
|
|
int intTemp = Convert.ToInt32(pIntValue);
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否数字字符串
|
|
/// </summary>
|
|
/// <param name="pValue">输入数值字符串</param>
|
|
/// <returns>参数为数字类型返回ture,否则返回false</returns>
|
|
public static bool IsNumeric(string pValue)
|
|
{
|
|
#region
|
|
Match m = RegNumber.Match(pValue);
|
|
return m.Success;
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否数字字符串可带正负号
|
|
/// </summary>
|
|
/// <param name="pValue">输入数值字符串</param>
|
|
/// <returns>参数为数字类型返回ture,否则返回false</returns>
|
|
public static bool IsNumericSign(string pValue)
|
|
{
|
|
#region
|
|
Match m = RegNumberSign.Match(pValue);
|
|
return m.Success;
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否是浮点数
|
|
/// </summary>
|
|
/// <param name="pValue">输入浮点数值字符串</param>
|
|
/// <returns>参数为浮点数类型返回ture,否则返回false</returns>
|
|
public static bool IsDecimal(string pValue)
|
|
{
|
|
#region
|
|
Match m = RegDecimal.Match(pValue);
|
|
return m.Success;
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否是浮点数可带正负号
|
|
/// </summary>
|
|
/// <param name="pValue">输入浮点数值字符串</param>
|
|
/// <returns>参数为浮点数型返回ture,否则返回false</returns>
|
|
public static bool IsDecimalSign(string pValue)
|
|
{
|
|
#region
|
|
Match m = RegDecimalSign.Match(pValue);
|
|
return m.Success;
|
|
#endregion
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 字符校验
|
|
|
|
/// <summary>
|
|
/// 是否英文字母字符串
|
|
/// </summary>
|
|
/// <param name="pValue">输入数值字符串</param>
|
|
/// <returns>是返回ture,否则返回false</returns>
|
|
public static bool IsEnString(string pValue)
|
|
{
|
|
#region
|
|
|
|
for (int intCnt = 0; intCnt < pValue.Length; intCnt++)
|
|
{
|
|
int intASC = (int)System.Text.Encoding.ASCII.GetBytes(FuncString.GetSubString(pValue, intCnt, 1))[0];
|
|
|
|
if (intASC > 65 || intASC > 122)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
#endregion
|
|
}
|
|
#endregion
|
|
|
|
#region 日期校验
|
|
/// <summary>是否日期</summary>
|
|
/// <param name="pInpDate">输入字符串</param>
|
|
/// <returns>true/false</returns>
|
|
public static bool IsDate(string pInpDate)
|
|
{
|
|
#region
|
|
string datestr = pInpDate;
|
|
string year, month, day;
|
|
string[] c ={ "/", "-", "." };
|
|
string cs = "";
|
|
for (int i = 0; i < c.Length; i++)
|
|
{
|
|
if (datestr.IndexOf(c[i]) > 0)
|
|
{
|
|
cs = c[i];
|
|
break;
|
|
}
|
|
|
|
};
|
|
|
|
if (cs != "")
|
|
{
|
|
year = datestr.Substring(0, datestr.IndexOf(cs));
|
|
if (year.Length != 4) { return false; };
|
|
datestr = datestr.Substring(datestr.IndexOf(cs) + 1);
|
|
|
|
month = datestr.Substring(0, datestr.IndexOf(cs));
|
|
if ((month.Length != 2) || (Convert.ToInt16(month) > 12))
|
|
{ return false; };
|
|
datestr = datestr.Substring(datestr.IndexOf(cs) + 1);
|
|
|
|
day = datestr;
|
|
if ((day.Length != 2) || (Convert.ToInt16(day) > 31)) { return false; };
|
|
|
|
return CheckDatePart(year, month, day);
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查年月日是否合法
|
|
/// </summary>
|
|
/// <param name="pYear">年</param>
|
|
/// <param name="pMonth">月</param>
|
|
/// <param name="pDay">日</param>
|
|
/// <returns></returns>
|
|
private static bool CheckDatePart(string pYear, string pMonth, string pDay)
|
|
{
|
|
int iyear = Convert.ToInt16(pYear);
|
|
int imonth = Convert.ToInt16(pMonth);
|
|
int iday = Convert.ToInt16(pDay);
|
|
if (iyear > 2099 || iyear < 1900) { return false; }
|
|
if (imonth > 12 || imonth < 1) { return false; }
|
|
if (iday > FuncDate.GetDaysOfMonth(iyear, imonth) || iday < 1) { return false; };
|
|
return true;
|
|
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region 正则表过式
|
|
/// <summary>
|
|
/// 正则表达式验证
|
|
/// </summary>
|
|
/// <param name="pValue">验证字符</param>
|
|
/// <param name="pString">正式表达式</param>
|
|
/// <returns>符合true不符合false</returns>
|
|
public static bool CheckRegEx(string pValue, string pString)
|
|
{
|
|
#region
|
|
Regex objAlphaPatt;
|
|
objAlphaPatt = new Regex(pString, RegexOptions.Compiled);
|
|
|
|
return objAlphaPatt.Match(pValue).Success;
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否为合法的电话号码
|
|
/// </summary>
|
|
/// <param name="pTel">输入字符串</param>
|
|
/// <returns>true/false</returns>
|
|
public static bool IsTel(string pTel)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否为合法的邮政编码
|
|
/// </summary>
|
|
/// <param name="pZip">输入字符串</param>
|
|
/// <returns>true/false</returns>
|
|
public static bool IsZip(string pZip)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否为合法的电子邮件
|
|
/// </summary>
|
|
/// <param name="pMail">输入字符串</param>
|
|
/// <returns>true/false</returns>
|
|
public static bool IsEmail(string pMail)
|
|
{
|
|
#region
|
|
Match m = RegEmail.Match(pMail);
|
|
return m.Success;
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否为合法的IP地址
|
|
/// </summary>
|
|
/// <param name="pIP"></param>
|
|
/// <returns></returns>
|
|
public static bool IsIP(string pIP)
|
|
{
|
|
#region
|
|
Match m = RegIP.Match(pIP);
|
|
return m.Success;
|
|
#endregion
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
|