[Android][工具類]CheckUtils

import android.text.TextUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 格式驗(yàn)證工具類
 * 如:手機(jī)格式驗(yàn)證,郵件格式驗(yàn)證
 *
 */
public class CheckUtils {

    /**
     * 手機(jī)號(hào):純數(shù)字 + 11位
     *
     * @param phone 手機(jī)號(hào)
     * @return 是否是錯(cuò)誤的手機(jī)號(hào)
     */
    public static boolean isWrongPhone(String phone) {
        if (TextUtils.isEmpty(phone) || !phone.startsWith("1"))
        return true;
        if (phone.length() != 11 || !isDigit(phone))
            return true;
        return false;
    }

    /**
     * 輸入字符串是否是數(shù)字
     *
     * @param str 輸入的字符串
     * @return 字符串是否是數(shù)字
     */
    public static boolean isDigit(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 輸入字符串是否包含特殊字符
     *
     * @param str 輸入的字符串
     * @return 是否包含特殊字符
     */
    public static boolean containsIllegalStr(String str) {
        String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }

    /**
     * 電子郵箱驗(yàn)證
     *
     * @param email 輸入的字符串
     * @return 是否符合郵箱格式
     */
    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容