C# 주민등록번호 15자리 및 18자리 인증
///
///
///
[Serializable]
public class IDCard
{
///
///
///
public string IDCardNum { get; set; }
///
///
///
public string Canton { get; private set; }
///
///
///
public DateTime Birthday { get; private set; }
///
/// (0- ;1- )
///
public int Gander { get; private set; }
///
///
///
public bool IsIDCard { get; private set; }
public IDCard() { }
public IDCard(string IDnumber)
{
this.IDCardNum = IDnumber;
}
///
///
///
///
///
public static IDCard Parse(string number)
{
IDCard idCard = new IDCard(number);
const int s5bits = 15;
const int s8bits = 18;
#region 15
if (number.Length == s5bits) //15
{
//
for (int i = 0; i '9'))
{
throw new FormatException(" ");
}
}
//
string birthday = "19" + number.Substring(6, 6);
string year = birthday.Substring(0, 4);
string month = birthday.Substring(4, 2);
string day = birthday.Substring(6, 2);
birthday = string.Format("{0}-{1}-{2}", year, month, day);
DateTime date = new DateTime();
if (DateTime.TryParse(birthday, out date))
{
idCard.Birthday = date;
}
else
{
throw new InvalidCastException(" ");
}
//
if ((number[s5bits - 1] == '0') || (number[s5bits - 1] % 2 == 0))
{
idCard.Gander = 0; //
}
else
{
idCard.Gander = 1; //
}
idCard.IsIDCard = true;
return idCard;
}
#endregion
#region 18
else if (number.Length == s8bits) //18
{
// 17
for (int i = 0; i '9'))
{
throw new FormatException(" ");
}
}
char end = number[s8bits - 1]; //
// 1 x X
if (end == 'x')
{
end = 'X';
number = number.Substring(0, s8bits - 1) + end;
}
if (!(end == 'X' || (end >= '0' && end <= '9')))
{
throw new FormatException(" ");
}
///
int num = 0;
char proof;
for (int i = 17; i > 0; i--)
{
num = num + (int)(Math.Pow(2, i) % 11) * (number[17 - i] - 48);
}
num %= 11;
switch (num)
{
case 0:
proof = '1';
break;
case 1:
proof = '0';
break;
case 2:
proof = 'X';
break;
default:
proof = (char)(12 - num + 48);
break;
}
if (end != proof) //
{
throw new FormatException(" ");
}
//
string birthday = number.Substring(6, 8);
string year = birthday.Substring(0, 4);
string month = birthday.Substring(4, 2);
string day = birthday.Substring(6, 2);
birthday = string.Format("{0}-{1}-{2}", year, month, day);
DateTime date = new DateTime();
if (DateTime.TryParse(birthday, out date))
{
idCard.Birthday = date;
}
else
{
throw new InvalidCastException(" ");
}
//
idCard.Canton = number.Substring(0, 6);
//
if ((number[16] == '0') || (number[16] % 2 == 0))
{
idCard.Gander = 0; //
}
else
{
idCard.Gander = 1; //
}
idCard.IsIDCard = true;
return idCard;
}
#endregion
else
{
throw new FormatException(" :" + number.Length);
}
}
public static bool TryParse(string number, out IDCard card)
{
IDCard idCard = null;
bool isIdCard = true;
try
{
Parse(number);
}
catch (Exception)
{
isIdCard = false;
}
card = idCard;
return isIdCard;
}
}
전재 대상:https://www.cnblogs.com/fanying/p/10919038.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.