• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

java 搞代码 4年前 (2022-01-05) 23次浏览 已收录 0个评论

这篇文章主要介绍了java使用正则表达式进行表单验证工具类,可以验证邮箱、手机号码、qq号码等方法,需要的朋友可以参考下

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);
    }
}

以上就是java正则

来源gaodai.ma#com搞##代!^码@网

表达式表单验证类工具类(验证邮箱、手机号码、qq号码等)的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java正则表达式表单验证类工具类(验证邮箱、手机号码、qq号码等)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址