转载请标明出处:
作者:大灰狼
博客:http://www.gold98.net/blog
主页:http://www.gold98.net
package com.zb.deal;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
public class Validate {
public Validate()
{
}
public static void main(String[] args) {
//Validate v=new Validate();
//v.isMobile("133-74554544"); //测试手机号码
//v.isEmail(null,"null"); //测试电子邮箱地址
//v.isPhone("123433333333", "null");//验证电话号码
//v.isChName("李明小"); //验证汉字
//v.isChcharacter("d-a_as",4,6);//验证字母数字汉字
//v.isOnlyCharacter("fa--f", 2, 6);//验证字母数字-_
//v.isSame(null,null);
}
public boolean isMobile(String mobile)
{
boolean result=false;
//String regexp1="[0-9]{3}\\S-?[0-9]{8}"; //正则表达式一
String regexp2="(130|131|132|133|134|135|136|137|138|139|159)\\-?[0-9]{8}$";//正则表达式二
PatternCompiler compiler=new Perl5Compiler();
Pattern pattern=null;
try{
pattern=compiler.compile(regexp2);
}catch(MalformedPatternException e)
{
e.printStackTrace();
}
PatternMatcher matcher=new Perl5Matcher();
if(matcher.contains(mobile, pattern))
{
System.out.println("手机号码验证成功!");
result=true;
}
else
{
result=false;
System.out.println("手机号码验证失败!");
}
return result;
}
public boolean isEmail(String email,String nullsign)
{
boolean result=false;
String regexp="^[0-9A-Za-z]{3,30}\\.?[0-9A-Za-z]{1,20}@{1}[0-9A-Za-z]{2,20}\\.+(com|net|cn|mobi|org)$"; //正则表达式
if(nullsign.equals("null"))
if(email==null||email.equals(""))
{
System.out.println("电子邮件验证成功!");
return true;
}
PatternCompiler compiler=new Perl5Compiler();
Pattern pattern=null;
try{
pattern=compiler.compile(regexp);
}catch(MalformedPatternException e)
{
e.printStackTrace();
}
PatternMatcher matcher=new Perl5Matcher();
if(matcher.contains(email, pattern))
{
System.out.println("电子邮件验证成功!");
result=true;
}
else
{
result=false;
System.out.println("电子邮件验证失败!");
}
return result;
}
//验证固定电话
public boolean isPhone(String phone,String nullsign)
{
boolean result=false;
String regexp1="^[0-9]{3,4}\\-{0,1}[0-9]{7,8}$"; //带区号的电话验证
//String regexp2="[0-9]{7,8}"; //不带区号的电话验证
if(nullsign.equals("null"))
if(phone==null||phone.equals(""))
{
System.out.println("联系电话验证成功!");
return true;
}
PatternCompiler compiler=new Perl5Compiler();
Pattern pattern=null;
try{
pattern=compiler.compile(regexp1);
}catch(MalformedPatternException e)
{
e.printStackTrace();
}
PatternMatcher matcher=new Perl5Matcher();
if(matcher.contains(phone, pattern))
{
System.out.println("电话号码验证成功!");
result=true;
}
else
{
result=false;
System.out.println("电话号码验证失败!");
}
return result;
}