18位身份证验证类+邮箱验证+数字验证

阅读更多

在Java代码中进行各种验证,欢迎补充:

 

身份证验证:

public class IDCard {
    // wi =2(n-1)(mod 11)
    final int[] wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
    // verify digit
    final int[] vi = { 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 };
    private int[] ai = new int[18];
    public IDCard() {
    }
    // verify
    public boolean Verify(String idcard) {
       if (idcard.length() == 15) {
           idcard = uptoeighteen(idcard);
       }
       if (idcard.length() != 18) {
           return false;
       }
       String verify = idcard.substring(17, 18);
       if (verify.equals(getVerify(idcard))) {
           return true;
       }
       return false;
    }
    // get verify
    public String getVerify(String eightcardid) {
       int remaining = 0;
       if (eightcardid.length() == 18) {
           eightcardid = eightcardid.substring(0, 17);
       }
       if (eightcardid.length() == 17) {
           int sum = 0;
           for (int i = 0; i < 17; i++) {
              String k = eightcardid.substring(i, i + 1);
              ai[i] = Integer.parseInt(k);
           }
           for (int i = 0; i < 17; i++) {
              sum = sum + wi[i] * ai[i];
           }
           remaining = sum % 11;
       }
       return remaining == 2 ? "X" : String.valueOf(vi[remaining]);
    }
    // 15 update to 18
    public String uptoeighteen(String fifteencardid) {
       String eightcardid = fifteencardid.substring(0, 6);
       eightcardid = eightcardid + "19";
       eightcardid = eightcardid + fifteencardid.substring(6, 15);
       eightcardid = eightcardid + getVerify(eightcardid);
       return eightcardid;
    }   
    public static void main(String[] args) {
       String idcard1 = "输入错误身份证号码";    
       String idcard2 = "输入正确身份证号码";    
       IDCard idcard = new IDCard();    
       System.out.println(idcard.Verify(idcard1));    
       System.out.println(idcard.Verify(idcard2)); 
    }
}

 

邮箱的验证:

/**
 * 验证输入的邮箱格式是否符合
 * 
 * @param email
 * @return 是否合法
 */
public static boolean emailFormat(String email) {
    boolean tag = true;
    final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    final Pattern pattern = Pattern.compile(pattern1);
    final Matcher mat = pattern.matcher(email);
    if (!mat.find()) {
       tag = false;
    }
    return tag;
}

 

三种数字验证方法:

//用JAVA自带的函数
public static boolean isNumeric1(String str) {
    for (int i = str.length(); --i >= 0;) {
       if (!Character.isDigit(str.charAt(i))) {
           return false;
       }
    }
    return true;
}
//正则表达式
public static boolean isNumeric2(String str) {
    Pattern pattern = Pattern.compile("[0-9]*");
    return pattern.matcher(str).matches();
}
//用ascii码
public static boolean isNumeric3(String str) {
    for (int i = str.length(); --i >= 0;) {
       int chr = str.charAt(i);
       if (chr < 48 || chr > 57)
           return false;
    }
    return true;
}

 

你可能感兴趣的:(正则表达式,验证,身份证,邮箱,数字)