身份驗證¶
public static bool CheckIDNO(string id)
{
int[] seed = new int[10]; //除了檢查碼外每個數字的存放空間
string[] charMapping = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "W", "Z", "I", "O" };
//A=10 B=11 C=12 D=13 E=14 F=15 G=16 H=17 J=18 K=19 L=20 M=21 N=22
//P=23 Q=24 R=25 S=26 T=27 U=28 V=29 X=30 Y=31 W=32 Z=33 I=34 O=35
string target = id.Substring(0, 1);
for (int index = 0; index < charMapping.Length; index++)
{
if (charMapping[index] == target)
{
index += 10;
seed[0] = index / 10; //10進制的高位元放入存放空間
seed[1] = (index % 10) * 9; //10進制的低位元*9後放入存放空間
break;
}
}
for (int index = 2; index < 10; index++)
{ //將剩餘數字乘上權數後放入存放空間
seed[index] = Convert.ToInt32(id.Substring(index - 1, 1)) * (10 - index);
}
//檢查是否符合檢查規則,10減存放空間所有數字和除以10的餘數的個位數字是否等於檢查碼
//(10 - ((seed[0] + .... + seed[9]) % 10)) % 10 == 身分證字號的最後一碼
return (10 - (seed.Sum() % 10)) % 10 == Convert.ToInt32(id.Substring(9, 1));
}
統編驗證¶
private bool CheckCompanyN(string CompanyId)
{
int[] Logic = new int[] { 1, 2, 1, 2, 1, 2, 4, 1 };
int addition, sum = 0, j = 0, x;
for (x = 0; x < Logic.Length; x++)
{
int no = Convert.ToInt32(CompanyId.Substring(x, 1));
j = no * Logic[x];
addition = ((j / 10) + (j % 10));
sum += addition;
}
if (sum % 10 == 0)
{
return true;
}
if (CompanyId.Substring(6, 1) == "7")
{
if (sum % 10 == 9)
{
return true;
}
}
return false;
}
Email驗證¶
public const string RegExString = @"^((([A-Za-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([A-Za-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([A-Za-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([A-Za-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([A-Za-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([A-Za-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([A-Za-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([A-Za-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([A-Za-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([A-Za-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$";
手機驗證¶
public const string RegExString = @"[0-9\#\+\-\(\)]";
Comments
comments powered by Disqus