Android EditText判断输入字符串的工具类集合,细节爆炸

本文分享了Android中用于EditText输入验证的工具类,包括中文验证、身份证号码验证、手机号码验证,以及如何在EditText中根据输入类型进行相应验证的示例代码。
摘要由CSDN通过智能技术生成

在这里插入图片描述

代码如下:

/**

  • 中文验证

  • @param str

  • @return

*/

public static boolean isAllChinese(String str){

if(isNullOrEmpty(str))return true;

String overseerInfo = “^([\u4e00-\u9fa5]|\ue82d)+$”;

Pattern pattern=Pattern.compile(overseerInfo);

Matcher matcher=pattern.matcher(str);

if(!matcher.matches()){

return false;

}

return true;

}

2.身份证号码

在这里插入图片描述

代码如下:

/**

*验证身份证

  • @param identNum

  • @return

*/

public static boolean isIdentNum(String identNum) {

//二代身份证的长度

if (identNum.length() != 18) {

return false;

}

//验证二代身份证的格式

Pattern pattern = Pattern.compile(“[0-9]{10}[0,1]{1}[0-9]{1}[0,1,2,3]{1}[0-9]{4}([0-9]|[X]){1}”);

if (!pattern.matcher(identNum).matches()) {

return false;

}

int year = Integer.parseInt(identNum.substring(6, 10));

int month = Integer.parseInt(identNum.substring(10, 12));

int day = Integer.parseInt(identNum.substring(12, 14));

//验证年份是否超出常规

if (year < 1800 || year > 2100) {

return false;

}

//验证月份是否有效

if (month < 1 || month > 12) {

return false;

}

//验证天数是否有效

int[] monthDayNum;

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

monthDayNum = new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31,30, 31 };

} else {

monthDayNum = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30, 31 };

}

if (day < 1 || day > monthDayNum[month - 1]) {

return false;

}

//验证因子是否有效

int[] factor = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5,8, 4, 2, 1 };

String[] parity = new String[] { “1”, “0”, “X”, “9”, “8”, “7”, “6”,“5”, “4”, “3”, “2” };

int sum = 0;

for (int i = 0; i < 17; i++) {

sum += Integer.parseInt(identNum.substring(i, i + 1)) * factor[i];

}

int bitIndex = sum % 11;

String checkBit=identNum.substring(17);;

if (!checkBit.equals(parity[bitIndex])) {

return false;

}

return true;

}

3.手机号或者电话

在这里插入图片描述

代码如下:

public static boolean isMobilePhone(CharSequence inputStr){

// String mobile = “^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$”;

String mobile = “^(1[3456789]+\d{9})$”;//最新的电话正则表达式与平台保持一致

Pattern pattern=Pattern.compile(mobile);

Matcher matcher=pattern.matcher(inputStr);

if(!matcher.matches()){

return false;

}

return true;

  • 28
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值