android 驗證一

來源:互聯網
上載者:User

標籤:android   style   blog   ar   color   sp   for   on   div   

public class RegularUtil {     public static boolean checkName(Activity context, String name) {        if (TextUtils.isEmpty(name) || name.length() < 3 || name.length() > 16 || !nameFormat(name)) {            AppMsg.makeText(context, "暱稱不符合規範,3-16個中英文字元、數字", AppMsg.STYLE_ALERT).show();            return false;        }        return true;    }     public static boolean checkHeight(Activity context, int height) {        if (height < 100 || height > 250) {            AppMsg.makeText(context, "身高超出正常範圍", AppMsg.STYLE_ALERT).show();            return false;        }        return true;    }     public static boolean checkWeight(Activity context, int weight) {        if (weight < 40 || weight > 250) {            AppMsg.makeText(context, "體重超出正常範圍", AppMsg.STYLE_ALERT).show();            return false;        }        return true;    }     public static boolean checkStepSize(Activity context, int stepSize) {        if (stepSize < 30 || stepSize > 150) {            AppMsg.makeText(context, "步長超出正常範圍", AppMsg.STYLE_ALERT).show();            return false;        }        return true;    }     public static boolean checkEmail(Activity context, String email) {        if (!emailFormat(email) || email.length() > 31) {            AppMsg.makeText(context, "郵箱格式不正確", AppMsg.STYLE_ALERT).show();            return false;        }        return true;    }     public static boolean checkPassword(Activity context, String password) {        if (!passwordFormat(password)) {            AppMsg.makeText(context, "密碼格式是6-15位英文字元、數字", AppMsg.STYLE_ALERT).show();            return false;        }        return true;    }     public static boolean checkPassword(Activity context, String password, String confirm) {        if (!checkPassword(context, password)) {            return false;        }        if (!password.equals(confirm)) {            AppMsg.makeText(context, "登入密碼設定不一致");            return false;        }        return true;    }     public static boolean checkCode(Activity context, String code) {        if (code.length() != 4) {            AppMsg.makeText(context, "請輸入正確的四位驗證碼", AppMsg.STYLE_ALERT).show();            return false;        }        return true;    }     public static boolean check(Activity context, String email, String password) {        if (!emailFormat(email) || email.length() > 31) {            AppMsg.makeText(context, "郵箱格式不正確", AppMsg.STYLE_ALERT).show();            return false;        }        if (!checkPassword(context, password)) {            return false;        }        return true;    }     private static boolean emailFormat(String email) {        Pattern pattern = Pattern.compile("^[A-Za-z\\d]+(\\.[A-Za-z\\d]+)*@([\\dA-Za-z](-[\\dA-Za-z])?)+(\\.{1,2}[A-Za-z]+)+$");        Matcher mc = pattern.matcher(email);        return mc.matches();    }     /**     * 以字母開頭,長度在3~16之間,只能包含字元、數字和底線(w)     *     * @param password     * @return     */    private static boolean passwordFormat(String password) {        Pattern pattern = Pattern.compile("^[\\@A-Za-z0-9\\!\\#\\$\\%\\^\\&\\*\\.\\~]{6,15}$");        Matcher mc = pattern.matcher(password);        return mc.matches();    }     public static boolean nameFormat(String name) {        Pattern pattern = Pattern.compile("^[\u4e00-\u9fa5A-Za-z0-9_]{3,16}$");        Matcher mc = pattern.matcher(name);        return mc.matches();    }     /**     * 擷取含雙位元組字元的字串位元組長度     *     * @param s     * @return     */    public static int getStringLength(String s) {        char[] chars = s.toCharArray();        int count = 0;        for (char c : chars) {            count += getSpecialCharLength(c);        }        return count;    }     /**     * 擷取字元長度:漢、日、韓文字元長度為2,ASCII碼等字元長度為1     *     * @param c     *            字元     * @return 字元長度     */    private static int getSpecialCharLength(char c) {        if (isLetter(c)) {            return 1;        } else {            return 2;        }    }     /**     * 判斷一個字元是Ascill字元還是其它字元(如漢,日,韓文字元)     *     * @param char c, 需要判斷的字元     * @return boolean, 返回true,Ascill字元     */    private static boolean isLetter(char c) {        int k = 0x80;        return c / k == 0 ? true : false;    }}

 

android 驗證一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.