手机邮箱验证工具类

package cn.view.viewlibrary.utils;

import android.text.TextUtils;

/**
 * 有效性校验类
 *
 * @author zhangxiaoyang create at 2019/8/19
 * @Description 描述:
 */
public class CheckUtils {
    /**
     * 邮箱格式验证
     *
     * @param strEmail
     * @return
     */
    public static boolean isEmail(String strEmail) {
        String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        if (TextUtils.isEmpty(strPattern)) {
            return false;
        } else {
            return strEmail.matches(strPattern);
        }
    }

    /**
     * 手机格式验证
     *
     * @param number
     * @return
     */
    public static boolean isMobile(String number) {
        /*
        移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
        联通:130、131、132、152、155、156、185、186
        电信:133、153、180、189、(1349卫通)
        总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9
        */
        //"[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。
        String num = "[1][358]\\d{9}";
        if (TextUtils.isEmpty(number)) {
            return false;
        } else {
            //matches():字符串是否在给定的正则表达式匹配
            return number.matches(num);
        }
    }
}

用法

if (!mobile){
    CommunUtils.toast("手机号输入不合法");
}else{
原创文章 63 获赞 7 访问量 6267

猜你喜欢

转载自blog.csdn.net/weixin_42416789/article/details/100099241