1. 程式人生 > >手機號正則表示式及輸入的內容只能輸入漢字

手機號正則表示式及輸入的內容只能輸入漢字

//工具類
public class MobileCheckUtil {

    /**
     * 手機號正則表示式
     * @param str
     * @return
     * @throws PatternSyntaxException
     */
    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        String PHONE_NUMBER_REG = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
        Pattern p = Pattern.compile(PHONE_NUMBER_REG);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 手機號用****號隱藏中間數字
     *
     * @param phone
     * @return
     */
    public static String settingphone(String phone) {
        String phone_s = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        return phone_s;
    }

    /**
     * 只能輸入漢字
     */
    public static void onlyInputChinese(EditText editText) {

        //只能輸入漢字  不能輸入特殊符號 英文
        InputFilter filter = new InputFilter() {
            public CharSequence filter(CharSequence source, int start, int end,
                                       Spanned dest, int dstart, int dend) {
                String speChat = "[`
[email protected]
#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; Pattern pattern = Pattern.compile(speChat); Matcher matcher = pattern.matcher(source.toString()); for (int i = start; i < end; i++) { if (!isChinese(source.charAt(i)) || matcher.find()) { return ""; } } return null; } }; editText.setFilters(new InputFilter[]{filter}); } /** * 只能輸入漢字 * * @param c * @return * @throws PatternSyntaxException */ public static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { return true; } return false; } }