码迷,mamicode.com
首页 > 移动开发 > 详细

Android中验证输入是否为汉字及手机号,邮箱验证,IP地址可用port号验证

时间:2017-05-23 13:17:48      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:lock   idt   空格   lfw   graphs   pos   isp   for   als   

1,验证是否为汉字

// 验证昵称
    private boolean verifyNickname() {
        String nickname = edt_username.getText().toString();
        if (nickname == null || nickname.length() == 0) {
            edt_username.setError("不能为空");
            return false;
        }
        int len = 0;
        char[] nickchar = nickname.toCharArray();
        for (int i = 0; i < nickchar.length; i++) {
            if (isChinese(nickchar[i])) {
                len += 2;
            } else {
                len += 1;
            }
        }
        if (len < 4 || len > 15) {
            edt_username.setError("正确的昵称应该为\n1、4-15个字符\n2、2-7个汉字\n3、不能是邮箱和手机号");
            return false;
        }
        return true;
    }

    private 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;
    }

2,验证手机号。邮箱

// 推断是否为手机号
    private boolean isPhone(String inputText) {
        Pattern p = Pattern
                .compile("^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$");
        Matcher m = p.matcher(inputText);
        return m.matches();
    }

    // 推断格式是否为email
    public 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();
    }

3,IP地址可用port号验证,可用port号(1024-65536)

private boolean MatcherIP(String ip) {
        Pattern pattern = Pattern.compile("^((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])"
                + "\\.((?

!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" + "\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" + "\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" + ":((102[4-9])|(10[3-9]\\d)|([1-5]\\d{3,4})|(6[0-4]\\d{3})|(65[0-4]\\d{2})|(655[0-2]\\d)|(6553[0-5]))$"); Matcher matcher = pattern.matcher(ip); return matcher.matches(); }



4,匹配/* 1234567890 */(“/*”开头,中间包括数字空格,以 “*/”结尾)

Pattern pattern = Pattern.compile("/*\\/*\\s*\\d*\\s*\\*/*");
Matcher matcher = pattern.matcher("正則表達式 /*  123   */ Hello World,正則表達式 Hello World");
//替换第一个符合正则的数据
System.out.println(matcher.replaceAll(""));


5 ,经常使用的eclipse搜索的正則表達式
1,Eclipse ctrl+f 打开查找框
2。选中 Regular expressions (正則表達式)

去掉/* */(eclipse)        /\*(.|[\r\n])*?\*/
去掉//(eclipse)             //.*$
去掉import(eclipse)     import.*$
去掉空行(eclipse)         ^\s*\n
去掉空行(ue)               %[ ^t]++^p

6,视频播放进度条中时间显示
//分钟
String am = String.format("%02d", tTotal / 60);
//秒
String as = String.format("%02d", tTotal % 60);
tiempoTotal.setText(am + ":" + as);







Android中验证输入是否为汉字及手机号,邮箱验证,IP地址可用port号验证

标签:lock   idt   空格   lfw   graphs   pos   isp   for   als   

原文地址:http://www.cnblogs.com/brucemengbm/p/6893368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!