Android 正则表达式验证手机和邮箱格式是否正确

demo例子:

 package it.com.cc;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.os.Bundle;

public class Demo4Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
boolean isPhone = isMobileNO("1684565463453");

boolean isEmail = isEmail("dthtterge@trytryvv.com");

boolean isnumber = isNumeric("564465");
System.out.println("isNumeric=" + isnumber + "isPhone=" + isPhone
+ "isEmail=" + isEmail + "qq===========");
}
       //判断手机格式是否正确
public boolean isMobileNO(String mobiles) {
Pattern p = Pattern
.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Matcher m = p.matcher(mobiles);

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();
}
       //判断是否全是数字
public boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
}
判断反馈信息的逻辑处理:

userInfoStr = userInfo.getText().toString().trim();
contentStr = content.getText().toString().trim();
if (!Utils.checkNetworkAvalible()) {
Toast.makeText(view.getContext(), "网络不可用,请检查网络设置!",
Toast.LENGTH_SHORT).show();
return;
}
if (contentStr != null && contentStr != ""
&& contentStr.length() > 0) {
if(!isNumeric(userInfoStr)&&!isEmail(userInfoStr)){
alerDialog("手机或邮箱格式不正确");
return;
}
if(isNumeric(userInfoStr)&&!isMobileNO(userInfoStr)){
alerDialog("手机或邮箱格式不正确");
return;
}
ThreadPoolWrap.getThreadPool().executeTask(runnable);
Toast.makeText(view.getContext(), "提交成功,感谢支持!",
Toast.LENGTH_SHORT).show();
userInfo.setText("");
content.setText("");
} else {
Toast.makeText(view.getContext(), "请输入反馈内容",
Toast.LENGTH_SHORT).show();
}
}

参考:

http://blog.sina.com.cn/s/blog_4b2fd9350100oa43.html
1.java验证IP地址:

Pattern pattern = Pattern.compile("\\b((?!\\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])\\b");

Matcher matcher = pattern.matcher("127.400.600.2"); //以验证127.400.600.2为例

System.out.println(matcher.matches());

2. java验证日期时间,解决润月:

Pattern pattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");

Matcher matcher = pattern.matcher("2000-02-29 23:59:59");

System.out.println(matcher.matches());

3.java验证邮箱格式:

Pattern pattern = Pattern.compile("^([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})(\\]?)$");

Matcher matcher = pattern.matcher("a@aa.com");

System.out.println(matcher.matches());

 

根据实际开发于2009年9月7日最新统计:
 中国电信发布中国3G号码段:中国联通185,186;中国移动188,187;中国电信189,180共6个号段。
 3G业务专属的180-189号段已基本分配给各运营商使用, 其中180、189分配给中国电信,187、188归中国移动使用,185、186属于新联通。
 中国移动拥有号码段:139138137136135134159158157(3G)、152151150188(3G)、187(3G);14个号段
 中国联通拥有号码段:130131132155156(3G)、186(3G)、185(3G);6个号段
 中国电信拥有号码段:133153189(3G)、180(3G);4个号码段
 移动:
     2G号段(GSM网络)有139,138,137,136,135,134(0-8),159,158,152,151,150
     3G号段(TD-SCDMA网络)有157,188,187
     147是移动TD上网卡专用号段.
 联通:
     2G号段(GSM网络)有130,131,132,155,156
     3G号段(WCDMA网络)有186,185
 电信:
     2G号段(CDMA网络)有133,153
     3G号段(CDMA网络)有189,180


检验手机号码和邮箱的类

package com.ccit.networld.ccs.common.util;

import java.io.IOException;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  

import org.apache.log4j.Logger;
 
public class ClassPathResource { 
 private static final Logger logger = Logger.getLogger(ClassPathResource.class);
    public static boolean isMobileNO(String mobiles){     
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");     
        Matcher m = p.matcher(mobiles);     
        logger.info(m.matches()+"---");     
        return m.matches();     
    } 
   
    public static boolean isEmail(String email){     
     String str="^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
        Pattern p = Pattern.compile(str);     
        Matcher m = p.matcher(email);     
        logger.info(m.matches()+"---");     
        return m.matches();     
    } 
    public static void main(String[] args) throws IOException {     
        System.out.println(ClassPathResource.isEmail("189666666666@1-89.co-m.cn"));     
    }  
} 

 

posted @ 2015-05-27 21:09  星辰之力  阅读(988)  评论(0编辑  收藏  举报