Android Regex驗證手機號、姓名(包含少數民族)、社會安全號碼,androidRegex

來源:互聯網
上載者:User

Android Regex驗證手機號、姓名(包含少數民族)、社會安全號碼,androidRegex

最近項目中新增的功能,需要對手機號、姓名、社會安全號碼等一些資訊進行驗證,最好的方法是通過Regex來驗證,網上查了一些資料,寫了這幾個工具方法。

1、驗證手機號

規則:第一位只能是1,第二位為3-8中的數字,3-11位為任意的數字

    /**     * 手機號號段校正,     第1位:1;     第2位:{3、4、5、6、7、8}任一數字;     第3—11位:0—9任一數字     * @param value     * @return     */    public static boolean isTelPhoneNumber(String value) {        if (value != null && value.length() == 11) {            Pattern pattern = Pattern.compile("^1[3|4|5|6|7|8][0-9]\\d{8}$");            Matcher matcher = pattern.matcher(value);            return matcher.matches();        }        return false;    }

2、驗證姓名這裡驗證姓名,使用者可以在輸入框內輸入任何東西,但是在點擊驗證的按鈕時,會調這個方法。

驗證規則是:姓名由漢字或漢字加“•”、"·"組成,而且,“點”只能有一個,“點”的位置不能在首位也不能在末尾,只有在漢字之間才會驗證通過。

    /**     * 驗證輸入的名字是否為“中文”或者是否包含“·”     */    public static boolean isLegalName(String name){        if (name.contains("·") || name.contains("•")){            if (name.matches("^[\\u4e00-\\u9fa5]+[·•][\\u4e00-\\u9fa5]+$")){                return true;            }else {                return false;            }        }else {            if (name.matches("^[\\u4e00-\\u9fa5]+$")){                return true;            }else {                return false;            }        }    }

3、驗證社會安全號碼

驗證社會安全號碼

規則是:由15位元字或18位元字(17位元字加“x”)組成,15位純數字沒什麼好說的,18位的話,可以是18位純數字,或者17位元字加“x”

    /**     * 驗證輸入的社會安全號碼是否合法     */    public static boolean isLegalId(String id){        if (id.toUpperCase().matches("(^\\d{15}$)|(^\\d{17}([0-9]|X)$)")){            return true;        }else {            return false;        }    }

以上Regex驗證結果,通過true和false返回

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.