字符串验证
Invalidate.cs
using System;
using System.Data;
using System.Configuration;
using System.Text.RegularExpressions;
/// <summary>
/// MyStringClass 的摘要说明
/// 用于信息验证
/// </summary>
namespace commClass
{
public class InfoValidata
{
private string str;
public InfoValidata(string strTemp)
{
str = strTemp;
}
public static implicit operator InfoValidata(string strTemp)
{
return new InfoValidata(strTemp);
}
public bool isOutOfSize(int size)
{
int strSize = System.Text.Encoding.Default.GetByteCount(str);
if (strSize > size)
return true;
return false;
}
#region 检查是否是数字
/// <summary>
/// 自然数
/// </summary>
public bool isNaturalNumber()
{
if (Regex.IsMatch(str, @"^\d*$"))
return true;
return false;
}
/// <summary>
/// 正整数
/// </summary>
public bool isPositiveNumber()
{
if (Regex.IsMatch(str, @"^[0-9]*[1-9][0-9]*$"))
return true;
return false;
}
/// <summary>
/// 负整数
/// </summary>
public bool isNegativeNumber()
{
if (Regex.IsMatch(str, @"^-[0-9]*[1-9][0-9]*$"))
return true;
return false;
}
/// <summary>
/// 负整数+0
/// </summary>
public bool isNotPositiveNumber()
{
if (Regex.IsMatch(str, @"^((-\d+)|(0+))$"))
return true;
return false;
}
/// <summary>
/// 整数
/// </summary>
public bool isNumber()
{
if (Regex.IsMatch(str, @"^-?\d+$"))
return true;
return false;
}
/// <summary>
/// 非负浮点数(正浮点数 + 0)
/// </summary>
public bool isNotNegativeFloat()
{
if (Regex.IsMatch(str, @"^\d+(\.\d+)?(\.\d+[e|E][\+|\-]\d+)?$"))
return true;
return false;
}
/// <summary>
/// 正浮点数
/// </summary>
public bool isPositiveFloat()
{
if (Regex.IsMatch(str, @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"))
return true;
return false;
}
/// <summary>
/// 非正浮点数(负浮点数 + 0)
/// </summary>
public bool isNotPositiveFloat()
{
if (Regex.IsMatch(str, @"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"))
return true;
return false;
}
/// <summary>
/// 负浮点数
/// </summary>
public bool isNegativeFloat()
{
if (Regex.IsMatch(str, @"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"))
return true;
return false;
}
/// <summary>
/// 浮点数
/// </summary>
public bool isFloat()
{
if (Regex.IsMatch(str, @"^(-?\d+)(\.\d+)?(\.\d+[e|E][\+|\-]\d+)?$"))
return true;
return false;
}
#endregion
/// <summary>
/// 由26个英文字母组成的字符串
/// </summary>
public bool isCharacter()
{
if (Regex.IsMatch(str, @"^[A-Za-z]+$"))
return true;
return false;
}
/// <summary>
/// 由26个英文字母的大写组成的字符串
/// </summary>
public bool isCharacterUpperCase()
{
if (Regex.IsMatch(str, @"^[A-Z]+$"))
return true;
return false;
}
/// <summary>
/// 由26个英文字母的小写组成的字符串
/// </summary>
public bool isCharacterLowerCase()
{
if (Regex.IsMatch(str, @"^[a-z]+$"))
return true;
return false;
}
/// <summary>
/// 由数字和26个英文字母组成的字符串
/// </summary>
public bool isCharacterAndNumber()
{
if (Regex.IsMatch(str, @"^[A-Za-z0-9]+$"))
return true;
return false;
}
/// <summary>
/// 由数字、26个英文字母或者下划线组成的字符串
/// </summary>
public bool isCharacter_Number()
{
if (Regex.IsMatch(str, @"^\w+$"))
return true;
return false;
}
/// <summary>
/// url
/// </summary>
public bool isUrl()
{
if (Regex.IsMatch(str, @"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$"))
return true;
return false;
}
/// <summary>
/// 电话号码
/// </summary>
public bool isPhoneNumber()
{
if (Regex.IsMatch(str, @"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?"))
return true;
return false;
}
public bool isMobilePhone()
{
if (Regex.IsMatch(str, @"^([+-]|\d{2,3})?(13[0-9]|15[0|3|6|8|9])\d{8}$"))
return true;
return false;
}
/// <summary>
/// IP地址
/// </summary>
public bool isIPAddress()
{
if (Regex.IsMatch(str, @"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$"))
return true;
return false;
}
/// <summary>
/// 年-月-日
/// </summary>
public bool isDate()
{
if (Regex.IsMatch(str, @"^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$"))
return true;
return false;
}
/// <summary>
/// Emil
/// </summary>
public bool isEmail()
{
if (Regex.IsMatch(str, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"))
return true;
return false;
}
/// <summary>
/// 身份证
/// </summary>
public bool isIDCard()
{
if (!Regex.IsMatch(str, @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$"))
return false;
DataSet dsProvince = new DataSet();
System.IO.TextReader reader = new System.IO.StringReader(m_strDicProvice);
dsProvince.ReadXml(reader);
string strMatch = str.Substring(0, 4);
dsProvince.Tables[0].PrimaryKey = new DataColumn[] { dsProvince.Tables[0].Columns["KEY"] };
if (!dsProvince.Tables[0].Rows.Contains(strMatch))
return false;
if (!ValidateCard(str))
return false;
return true;
}
private bool ValidateCard(string _Code)
{
bool IDCheck = true;
string Ai = string.Empty;
int intLength = _Code.Trim().Length;
int intYear = 0;
int intMonth = 0;
int intDay = 0;
string strNumber = "1,0,X,9,8,7,6,5,4,3,2";
string strWi = "7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2";
string strChecker = "1,9,8,7,6,5,4,3,2,1,1";
string[] arrVerifyCode = strNumber.Split(',');
string[] Wi = strWi.Split(',');
string[] Checker = strChecker.Split(',');
if (intLength == 15)
{
Ai = _Code;
intYear = int.Parse(Ai.Substring(6, 2));
intMonth = int.Parse(Ai.Substring(8, 2));
intDay = int.Parse(Ai.Substring(10, 2));
if (intYear < 49)
{
IDCheck = false;
return false;
}
if (intMonth > 12 || intDay > 31)
{
IDCheck = false;
return false;
}
return true;
}
Ai = _Code.Substring(0, 17);
intYear = int.Parse(Ai.Substring(6, 4));
intMonth = int.Parse(Ai.Substring(10, 2));
intDay = int.Parse(Ai.Substring(12, 2));
if (intYear < 1949 || intYear > 2010)
{
IDCheck = false;
return false;
}
if (intMonth > 12 || intDay > 31)
{
IDCheck = false;
return false;
}
int i;
int TotalmulAiWi = 0;
for (i = 0; i <= 16; i++)
{
TotalmulAiWi = TotalmulAiWi + int.Parse(Ai.Substring(i, 1)) * int.Parse(Wi[i]);
}
int modValue = TotalmulAiWi % 11;
string strVerifyCode = arrVerifyCode[modValue];
Ai = Ai + strVerifyCode;
if (Ai.Trim().Length == 18 && _Code != Ai)
{
IDCheck = false;
return false;
}
return true;
}
}
}