当前位置: 移动技术网 > IT编程>开发语言>Java > java正则表达式表单验证类工具类(验证邮箱、手机号码、qq号码等)

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

中国价值,河北省地图高清版,马丁的早晨下载

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 = pattern.compile(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);
    }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网