正则效验

public class AutoCheckEditText extends androidx.appcompat.widget.AppCompatEditText implements TextWatcher {
//手机校验类型
public static final int TYPE_OF_MOBILE = 0xb0;
//座机校验类型
public static final int TYPE_OF_TEL = 0xb1;
//邮箱校验类型
public static final int TYPE_OF_EMAIL = 0xb2;
//url校验类型
public static final int TYPE_OF_URL = 0xb3;
//汉字校验类型
public static final int TYPE_OF_CHZ = 0xb4;
//用户名校验类型
public static final int TYPE_OF_USERNAME = 0xb5;

//用户自定义
public static final int TYPE_OF_USER_DEFINE = 0xbb;
private int type;
private Drawable successDrawable;
private Drawable unsuccessDrawable;
private String userRegx;
private boolean isMatch;

public AutoCheckEditText(Context context) {
super(context);
}

public AutoCheckEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}

public AutoCheckEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}


/**
* @param typ 要校验的类型
* @param success 匹配成功时的图标
* @param unsuccess 匹配失败时的图标
*/
public void creatCheck(int typ, Drawable success, Drawable unsuccess) {
type = typ;
successDrawable = success;
successDrawable.setBounds(0, 0, successDrawable.getMinimumWidth(), successDrawable.getMinimumHeight());
unsuccessDrawable = unsuccess;
unsuccessDrawable.setBounds(0, 0, unsuccessDrawable.getMinimumWidth(), unsuccessDrawable.getMinimumHeight());
this.addTextChangedListener(this);
}

/**
* @param typ 要校验的类型
* @param success 匹配成功时的图标
* @param unsuccess 匹配失败时的图标
*/
public void creatCheck(int typ, Drawable success, Drawable unsuccess, String userRegex) {
creatCheck(typ, success, unsuccess);
this.userRegx = userRegex;
}

public boolean isMatch() {
return isMatch;
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void afterTextChanged(Editable s) {
if (s != null && s.length() > 0) {
if (type == TYPE_OF_MOBILE) {
isMatch = Check.isMobile(s.toString());
} else if (type == TYPE_OF_TEL) {
isMatch = Check.isTel(s.toString());
} else if (type == TYPE_OF_EMAIL) {
isMatch = Check.isEmail(s.toString());
} else if (type == TYPE_OF_URL) {
isMatch = Check.isURL(s.toString());
} else if (type == TYPE_OF_CHZ) {
isMatch = Check.isChz(s.toString());
} else if (type == TYPE_OF_USERNAME) {
isMatch = Check.isUsername(s.toString());
} else if (type == TYPE_OF_USER_DEFINE) {
isMatch = Check.isMatch(userRegx, s.toString());
} else {
return;
}
if (isMatch) {
setCompoundDrawables(null, null, successDrawable, null);
} else {
setCompoundDrawables(null, null, unsuccessDrawable, null);
}
} else {
setCompoundDrawables(null, null, null, null);
}
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println();
}
}



public class Check {
/**
* 验证手机号和电话号
*/
private static final String REGEX_MOBILE = "^1[34578][0-9]\\d{8}$|^0\\d{2,3}[- ]?\\d{7,8}";
/**
* 验证座机号,正确格式:xxx/xxxx-xxxxxxx/xxxxxxxx
*/
private static final String REGEX_TEL = "^0\\d{2,3}[- ]?\\d{7,8}";
/**
* 验证邮箱
*/
private static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
/**
* 验证url
*/
private static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?";
/**
* 验证汉字
*/
private static final String REGEX_CHZ = "^[\\u4e00-\\u9fa5]+$";
/**
* 验证用户名,取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾,用户名必须是6-20位
*/
private static final String REGEX_USERNAME = "^[\\w\\u4e00-\\u9fa5]{6,20}(?<!_)$";
/**
* 验证IP地址
*/
private static final String REGEX_IP = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
/**
* 验证数字
*/
private static final String REGEX_NUM = "^(-?\\d+)(\\.\\d+)?$";
/**
* @param string 待验证文本
* @return 是否符合手机号格式
*/
public static boolean isMobile(String string) {
return isMatch(REGEX_MOBILE, string);
}

/**
* @param string 待验证文本
* @return 是否符合座机号码格式
*/
public static boolean isTel(String string) {
return isMatch(REGEX_TEL, string);
}

/**
* @param string 待验证文本
* @return 是否符合邮箱格式
*/
public static boolean isEmail(String string) {
return isMatch(REGEX_EMAIL, string);
}

/**
* @param string 待验证文本
* @return 是否符合网址格式
*/
public static boolean isURL(String string) {
return isMatch(REGEX_URL, string);
}

/**
* @param string 待验证文本
* @return 是否符合汉字
*/
public static boolean isChz(String string) {
return isMatch(REGEX_CHZ, string);
}

/**
* @param string 待验证文本
* @return 是否符合用户名
*/
public static boolean isUsername(String string) {
return isMatch(REGEX_USERNAME, string);
}

/**
* @param regex 正则表达式字符串
* @param string 要匹配的字符串
* @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
*/
public static boolean isMatch(String regex, String string) {
if (string == null || string.length() == 0) {
return false;
}
return Pattern.matches(regex, string);
}

/**
* 是否是数字
* @param msg
* @return
*/
public static boolean isNumber(String msg) {
Pattern compile = compile(REGEX_NUM);
return compile.matcher(msg).matches();
}
}


//验证通过图标
Drawable successDrable = ResourcesCompat.getDrawable(getResources(),
R.drawable.ic_check_sel, getApplicationContext().getTheme());
//验证失败图标
Drawable unsuccessDrable = ResourcesCompat.getDrawable(getResources(),
R.mipmap.img_failure, getApplicationContext().getTheme());
//邮箱验证
mEtEmailSet.creatCheck(AutoCheckEditText.TYPE_OF_EMAIL, successDrable, unsuccessDrable);
//手机验证
mEtPhoneSet.creatCheck(AutoCheckEditText.TYPE_OF_MOBILE, successDrable, unsuccessDrable);

if (!TextUtils.isEmpty(mEtPhoneSet.getText())) {
if (!mEtPhoneSet.isMatch()) {
Toast.makeText(CustmomerEditAddActivity.this, "请输入正确手机号后提交",
Toast.LENGTH_LONG).show();
return;
}
}

posted on 2020-07-03 16:05  带镐伤的土豆  阅读(113)  评论(0编辑  收藏  举报

导航