700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java 传真 正则表达式_java正则表达式表单验证类工具类(验证邮箱 手机号码 qq号码等)...

java 传真 正则表达式_java正则表达式表单验证类工具类(验证邮箱 手机号码 qq号码等)...

时间:2023-02-24 04:24:37

相关推荐

java 传真 正则表达式_java正则表达式表单验证类工具类(验证邮箱 手机号码 qq号码等)...

package util;

import java.util.regex.matcher;

import java.util.regex.pattern;

/**

* 使用正则表达式进行表单验证

*

*/

public class regexvalidateutil {

static boolean flag = false;

static string regex = "";

public static boolean check(string str, string regex) {

try {

pattern pattern = pile(regex);

matcher matcher = pattern.matcher(str);

flag = matcher.matches();

} catch (exception e) {

flag = false;

}

return flag;

}

/**

* 验证非空

*

* @param email

* @return

*/

public static boolean checknotemputy(string notemputy) {

regex = "^\\s*$";

return check(notemputy, regex) ? false : true;

}

/**

* 验证邮箱

*

* @param email

* @return

*/

public static boolean checkemail(string email) {

string regex = "^\\w+[-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$ ";

return check(email, regex);

}

/**

* 验证手机号码

*

* 移动号码段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147

* 联通号码段:130、131、132、136、185、186、145

* 电信号码段:133、153、180、189

*

* @param cellphone

* @return

*/

public static boolean checkcellphone(string cellphone) {

string regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";

return check(cellphone, regex);

}

/**

* 验证固话号码

*

* @param telephone

* @return

*/

public static boolean checktelephone(string telephone) {

string regex = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";

return check(telephone, regex);

}

/**

* 验证传真号码

*

* @param fax

* @return

*/

public static boolean checkfax(string fax) {

string regex = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";

return check(fax, regex);

}

/**

* 验证qq号码

*

* @param qq

* @return

*/

public static boolean checkqq(string qq) {

string regex = "^[1-9][0-9]{4,} $";

return check(qq, regex);

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。