Commits

Inside Zhou committed a9072e9

增加身份证有效性验证的方法。

Comments (0)

Files changed (2)

Inside.Util.csproj

     <Compile Include="MiscExtension.cs" />
     <Compile Include="ObjectExtension.cs" />
     <Compile Include="ObjectHelper.cs" />
+    <Compile Include="PersonalIDHelper.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Sql\Condition.cs" />
     <Compile Include="Sql\DbDataObjectExtension.cs" />

PersonalIDHelper.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Inside.Util {
+	public class PersonalIDHelper {
+		public static bool Validate(string id) {
+			switch(id.Length) {
+				case 15:
+					return true;
+				case 18:
+					var lastCheck = id[17];
+
+					try {
+						//系数的算法是(2^(18-从1开始算起的序号)) mod 11
+						var factors = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
+						var checkIndex = 0;
+						for(int index = 0;index < 17;++index) {
+							checkIndex += int.Parse(id[index].ToString()) * factors[index];
+						}
+						checkIndex %= 11;
+
+						return lastCheck == new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }[checkIndex];
+					}
+					catch(FormatException) {
+						return false;
+					}
+					catch(Exception e) {
+						throw e;
+					}
+				default:
+					return false;
+			}
+		}
+	}
+}