import java.io.*;import java.lang.*;public class VerifyIpCard { public static void main(String[] args) throws IOException { System.out.println("please input the id card number:"); BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); String ww = s.readLine(); if (ww.length() == 15) { System.out.println("It's the new ID card!"); getInfof(ww); Convert(ww); } else if (ww.length() == 18) { System.out.println("It's the new ID card!"); getInfoe(ww); } else System.out.println("The length of the ID Card is not RIGHT!"); } static void getInfof(String s) { String M = new String(s); int y = Integer.parseInt(M.substring(6, 8)); int m = Integer.parseInt(M.substring(8, 10)); int d = Integer.parseInt(M.substring(10, 12)); if (m < 1 || m > 12 || d < 1 || d > 31 || ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30) || (m == 2 && (((y + 1900) % 4 > 0 && d > 28) || d > 29))) { System.out.println("身分証出身年月エラー!"); } else { System.out.println("故郷:" + M.substring(0, 6)); System.out.println("年月日:" + y + "年" + m + "月" + d + "日"); } int sex = Integer.parseInt(M.substring(14, 15)); if (sex % 2 == 0) { System.out.println("该居民为:女性"); } else { System.out.println("该居民为:男性"); } } static void getInfoe(String s) { String M = new String(s); int y = Integer.parseInt(M.substring(6, 10)); int m = Integer.parseInt(M.substring(10, 12)); int d = Integer.parseInt(M.substring(12, 14)); int[] xx = { 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 }; char[] yy = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; int mm = 0; int[] gg = new int[18]; if (y < 1900 || m < 1 || m > 12 || d < 1 || d > 31 || ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30) || (m == 2 && ((y % 4 > 0 && d > 28) || d > 29))) { System.out.println("該居民身份證出生年月日錯誤!"); } else { System.out.println("该居民出生地:" + M.substring(0, 6)); System.out.println("该居民出生时间为:" + y + "年" + m + "月" + d + "日"); } int sex = Integer.parseInt(M.substring(16, 17)); if (sex % 2 == 0) { System.out.println("该居民为:女性"); } else { System.out.println("该居民为:男性"); } for (int i = 1; i < 18; i++) { int j = 17 - i; gg[i - 1] = Integer.parseInt(M.substring(j, j + 1)); } for (int i = 0; i < 17; i++) { mm += xx[i] * gg[i]; } mm = mm % 11; char c = M.charAt(17); if (c == yy[mm]) System.out.println("该居民身份证是真的"); else System.out.println("该居民身份证是假的(校验码有误)"); } static void Convert(String s) throws IOException { StringBuffer sad = new StringBuffer(s); sad.insert(6, "19"); int[] xx = { 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 }; char[] yy = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; int mm = 0; int[] gg = new int[18]; for (int i = 1; i < 18; i++) { int j = 17 - i; gg[i - 1] = Integer.parseInt(sad.substring(j, j + 1)); } for (int i = 0; i < 17; i++) { mm += xx[i] * gg[i]; } mm = mm % 11; System.out.println("该居民身份证是新的身份证" + sad + yy[mm]); } } 这个可以验证
这种东西当然是在前台判断吧?格式采用js判断 内容用ajax判断
swing的话 public void setInputVerifier(javax.swing.InputVerifier);public abstract class javax.swing.InputVerifier extends java.lang.Object{ public javax.swing.InputVerifier(); public abstract boolean verify(javax.swing.JComponent); public boolean shouldYieldFocus(javax.swing.JComponent); }在 verify方法里 调用验证输入的身份证号。
import java.io.*;import java.lang.*;public class VerifyIpCard { public static void main(String[] args) throws IOException { System.out.println("please input the id card number:"); BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); String ww = s.readLine(); if (ww.length() == 15) { System.out.println("It's the new ID card!"); getInfof(ww); Convert(ww); } else if (ww.length() == 18) { System.out.println("It's the new ID card!"); getInfoe(ww); } else System.out.println("The length of the ID Card is not RIGHT!"); } static void getInfof(String s) { String M = new String(s); int y = Integer.parseInt(M.substring(6, 8)); int m = Integer.parseInt(M.substring(8, 10)); int d = Integer.parseInt(M.substring(10, 12)); if (m < 1 || m > 12 || d < 1 || d > 31
|| ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30) || (m == 2 && (((y + 1900) % 4 > 0 && d > 28) || d > 29))) {
System.out.println("身分証出身年月エラー!");
} else { System.out.println("故郷:" + M.substring(0, 6)); System.out.println("年月日:" + y + "年" + m + "月" + d + "日");
} int sex = Integer.parseInt(M.substring(14, 15)); if (sex % 2 == 0) {
System.out.println("该居民为:女性");
} else {
System.out.println("该居民为:男性"); } } static void getInfoe(String s) { String M = new String(s); int y = Integer.parseInt(M.substring(6, 10)); int m = Integer.parseInt(M.substring(10, 12)); int d = Integer.parseInt(M.substring(12, 14)); int[] xx = { 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 }; char[] yy = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; int mm = 0; int[] gg = new int[18]; if (y < 1900 || m < 1 || m > 12 || d < 1 || d > 31
|| ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30) || (m == 2 && ((y % 4 > 0 && d > 28) || d > 29))) {
System.out.println("該居民身份證出生年月日錯誤!");
} else { System.out.println("该居民出生地:" + M.substring(0, 6)); System.out.println("该居民出生时间为:" + y + "年" + m + "月" + d + "日");
} int sex = Integer.parseInt(M.substring(16, 17)); if (sex % 2 == 0) {
System.out.println("该居民为:女性");
} else {
System.out.println("该居民为:男性"); } for (int i = 1; i < 18; i++) {
int j = 17 - i; gg[i - 1] = Integer.parseInt(M.substring(j, j + 1)); } for (int i = 0; i < 17; i++) {
mm += xx[i] * gg[i];
} mm = mm % 11; char c = M.charAt(17); if (c == yy[mm]) System.out.println("该居民身份证是真的"); else System.out.println("该居民身份证是假的(校验码有误)"); } static void Convert(String s) throws IOException { StringBuffer sad = new StringBuffer(s); sad.insert(6, "19"); int[] xx = { 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 }; char[] yy = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; int mm = 0; int[] gg = new int[18]; for (int i = 1; i < 18; i++) {
int j = 17 - i; gg[i - 1] = Integer.parseInt(sad.substring(j, j + 1)); } for (int i = 0; i < 17; i++) {
mm += xx[i] * gg[i];
} mm = mm % 11; System.out.println("该居民身份证是新的身份证" + sad + yy[mm]);
}
}
这个可以验证
内容用ajax判断
public javax.swing.InputVerifier();
public abstract boolean verify(javax.swing.JComponent);
public boolean shouldYieldFocus(javax.swing.JComponent);
}在 verify方法里 调用验证输入的身份证号。