- 判断输入的是不是月份:
publicstatic bool IsMonthNumber(string str_number)
{
returnSystem.Text.RegularExpressions.Regex.IsMatch(str_number,@"^(0?[1-9]|1[0-2])$");
}
/// <summary>
/// 验证身份证号是否合法
/// </summary>
/// <paramname="Id"></param>
/// <returns></returns>
public static bool CheckIDCard(stringId)
{
//长度为18位的处理方法
if (Id.Length == 18)
{
bool check = CheckIDCard18(Id);
return check;
}
//长度为15位的处理方法
else if (Id.Length == 15)
{
bool check = CheckIDCard15(Id);
return check;
}
//长度不是18位或15位直接返回false
else
{
return false;
}
}
/// <summary>
/// 验证18位的身份证号
/// </summary>
/// <paramname="Id"></param>
/// <returns></returns>
private static boolCheckIDCard18(string Id)
{
long n = 0;
if (long.TryParse(Id.Remove(17),out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x','0').Replace('X', '0'), out n) == false)
{
//数字验证
return false;
}
string address ="11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2))== -1)
{
//省份验证
return false;
}
string birth = Id.Substring(6,8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, outtime) == false)
{
//生日验证
return false;
}
string[] arrVarifyCode =("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] wi =("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] ai =Id.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += int.Parse(wi[i]) *int.Parse(ai[i].ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] !=Id.Substring(17, 1).ToLower())
{
//校验码验证
return false;
}
//符合GB11643-1999标准
return true;
}
/// <summary>
/// 验证15位的身份证号
/// </summary>
/// <paramname="Id"></param>
/// <returns></returns>
private static boolCheckIDCard15(string Id)
{
long n = 0;
if (long.TryParse(Id, out n) ==false || n < Math.Pow(10, 14))
{
//数字验证
return false;
}
string address ="11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2))== -1)
{
//省份验证
return false;
}
string birth = Id.Substring(6,6).Insert(4, "-").Insert(2, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, outtime) == false)
{
//生日验证
return false;
}
//符合15位身份证标准
return true;
}
/// <summary>
/// 对姓名进行验证
/// </summary>
/// <paramname="name"></param>
public static boolNameLengthVerify(string name)
{
int n = 0;
foreach (char ch in name)
{
if (ch >= 0x4e00 &&ch <= 0x9fa5)
{
n++;//n对汉字进行计数
}
else
{
return false;
}
}
if (n < 2 || n > 6)
{
return false;
}
return true;
}
/// <summary>
/// 判断输入框中输入的是否是数字
/// </summary>
/// <paramname="str_number"></param>
/// <returns></returns>
public static bool IsNumber(stringstr_number)
{
returnSystem.Text.RegularExpressions.Regex.IsMatch(str_number,@"^[0-9]*$");
}
}
- 判断输入的姓名是不是合法:
///<summary>
/// 输入姓名后判断姓名是否合法
///</summary>
<paramname="sender"></param>
<paramname="e"></param>
private void textEditName_Leave(objectsender, EventArgs e)
{
if(!CommonOperator.NameLengthVerify(textEditName.Text))
{
FormFactory.ShowMessageBoxErrorDialog(this, "请检查姓名输入是否有误!");
textEditName.EditValue =string.Empty;
}
}
- 判断证件号是不是合法:
/// <summary>
/// 查询时判断证件号是否合法
/// </summary>
/// <paramname="sender"></param>
/// <paramname="e"></param>
private voidtextEditIdnoLook_Leave(object sender, EventArgs e)
{
//判断证件号是不是合法
if(!string.IsNullOrEmpty(textEditIdnoLook.Text))
{
if((comboBoxEditIdtypeLook.Text == "身份证")&& (!CommonOperator.CheckIDCard(textEditIdnoLook.Text)))
{
FormFactory.ShowMessageBoxErrorDialog(this, "请再次确认证件号是否正确!");
this.textEditIdnoLook.EditValue = string.Empty;
}
}
}