首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

java正则验证电话,手机,信箱,日期,金额

2013-11-09 
java正则验证电话,手机,邮箱,日期,金额http://www.cnblogs.com/xiaomuv587/archive/2013/01/24/2875191.ht

java正则验证电话,手机,邮箱,日期,金额

http://www.cnblogs.com/xiaomuv587/archive/2013/01/24/2875191.html

?

java正则验证电话,手机,邮箱,日期,金额

?

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class Check {
??? // 判断电话
??? public static boolean isTelephone(String phonenumber) {
??????? String phone = "0\\d{2,3}-\\d{7,8}";
??????? Pattern p = Pattern.compile(phone);
??????? Matcher m = p.matcher(phonenumber);
??????? return m.matches();
??? }

??? // 判断手机号
??? 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);
??????? 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);
??????? return m.matches();
??? }

??? // 判断日期格式:yyyy-mm-dd

??? public static boolean isValidDate(String sDate) {
??????? String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
??????? String datePattern2 = "^((\\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]))))))";
??????? if ((sDate != null)) {
??????????? Pattern pattern = Pattern.compile(datePattern1);
??????????? Matcher match = pattern.matcher(sDate);
??????????? if (match.matches()) {
??????????????? pattern = Pattern.compile(datePattern2);
??????????????? match = pattern.matcher(sDate);
??????????????? return match.matches();
??????????? } else {
??????????????? return false;
??????????? }
??????? }
??????? return false;
??? }
??? //验证金额
??? public static boolean isNumber(String str)
??? {
??????? java.util.regex.Pattern pattern=java.util.regex.Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后一位的数字的正则表达式
??????? java.util.regex.Matcher match=pattern.matcher(str);
??????? if(match.matches()==false)
??????? {
?????????? return false;
??????? }
??????? else
??????? {
?????????? return true;
??????? }
??? }
}

热点排行