故先要整清楚现在已经开放了多少个号码段,国家号码段分配如下:
移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
联通:130、131、132、152、155、156、185、186
电信:133、153、180、189、(1349卫通)
那么现在就可以正则匹配测试了,
源码import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ClassPathResource {
public static boolean isMobileNO(String mobiles){
Pattern p =
pile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Matcher m = p.matcher(mobiles);
System.out.println(m.matches()+"---");
return m.matches();
}
public static void main(String[] args) throws IOException
{
System.out.println(ClassPathResource.isMobileNO("1155153"));
}
}
第二种方法:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String value="手机号";
String regExp = "^[1]([3][0-9]{1}|59|58|88|89)[0-9]{8}$";
Pattern p = pile(regExp);
Matcher m = p.matcher(value);
return m.find();//boolean
邮编号码:xxx.xxxx(都为数字)固定电话号码:xx-xxxx-xxxx(都为数字)手机号码:xxx-xxxx-xxxx(都为数字)一般来说,基本的校验功能留给页面去处理,可以减轻服务器的负担。建议这些功能放在页面完成。java中的正则表达式:importorg.apache.oro.text.regex.MalformedPatternException;importorg.apache.oro.text.regex.Pattern;importorg.apache.oro.text.regex.PatternCompiler;importorg.apache.oro.text.regex.PatternMatcher;importorg.apache.oro.text.regex.Perl5Compiler;importorg.apache.oro.text.regex.Perl5Matcher;publicstaticbooleancheckFomatNumber(Stringnumber){/if(!s2.match(type)){alert("移动电话的格式必须是xxx-xxxx-xxxx,且均为数字!");return(false);}return(true);}