表单提交后台正则表达式验证

package com.smartdot.dcu;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidUtil {

    /**
     * 验证字符串的长度
     * 
     * @param str
     * @param l
     * @return
     */
    public static boolean validLength(String str, int length) {
        if (!isNull(str)&&str.length() <= length) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 限定长度的数字
     * @param str
     * @return
     */
    public static boolean isNumber(String str,int length) {
        if(isNull(str))return false;
        String re = "^[0-9]{"+length+"}";
//      String reg = "^[\\-\\+]?(([0-9]+)([\\.,]([0-9]+))?|([\\.,]([0-9]+))?)$";
        Pattern p = Pattern.compile(re);
        Matcher m = p.matcher(str);
        boolean d = m.matches();
        return d;
    }
    /**
     * 判断字符串是否为纯数字,0可以在首位
     * @param str
     * @return
     */
    public static boolean isNumbers(String str){
        String re = "^\\d+$";
        Pattern p = Pattern.compile(re);
        Matcher m = p.matcher(str);
        boolean d = m.matches();
        return d;
    }

    /**
     * 手机号码验证
     * 
     * @param str
     * @return
     */
    public static boolean isPhone(String str) {
        if(isNull(str))return false;
        String re = "^[1][3,4,5]+\\d{9}";
        Pattern p = Pattern.compile(re);
        Matcher m = p.matcher(str);
        boolean d = m.matches();
        return d;
    }

    /**
     * 是否是html代码
     * @param str
     * @return
     */
    public static boolean checkHtmlTag(String str) {
        if(isNull(str))return false;
        String re = "<[^>]+>";
        Pattern p = Pattern.compile(re);
        Matcher m = p.matcher(str);
        boolean d = m.matches();
        return d;
    }

    /**
     * 是否是日期格式 YYYY-MM-DD
     * 
     * @param str
     * @return
     */
    public static boolean isDate(String str) {
        if(isNull(str))return false;
        String re = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
        Pattern p = Pattern.compile(re);
        Matcher m = p.matcher(str);
        boolean d = m.matches();
        return d;
    }

    /**
     * 是否为正整数
     * 
     * @param str
     * @return
     */
    public static boolean isInteger(String str) {
        if(isNull(str))return false;
        String re = "^\\d*[1-9]\\d*$";
        Pattern p = Pattern.compile(re);
        Matcher m = p.matcher(str);
        boolean d = m.matches();
        return d;
    }

    /**
     * 区号+座机号码+分机号码
     * 
     * @param fixedPhone
     * @return
     */
    public static boolean isFixedPhone(String str) {
        if(isNull(str))return false;
        String reg = "(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)|"
                + "(?:(86-?)?(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)";
        String re = "^0\\d{2,3}-?\\d{7,8}$";
        return Pattern.matches(re, str);    
    }

    /**
     * 数字,且大于1,可以为小数
     * @param str
     * @return
     */
    public static boolean checkYear(String str){
        if(isNull(str))return false;
        String reg = "([2-9]+(\\.\\d+)?)|(1\\d+(\\.\\d+)?)|(1\\.\\d*[1-9]+)";
        return Pattern.matches(reg, str);
    }
    /**
     * 是否含有特殊字符
     * @param str
     * @return
     */
    public static boolean checkString(String str){
        String re ="[^<>%;()+&]{1,}";
        Pattern p = Pattern.compile(re);
        Matcher m = p.matcher(str);
        boolean d = m.matches();
        return d;
    }

    public static boolean isNull(String str){
        if(str==null){
            return true;
        }else{
            return false;
        }
    }
    public static void main(String args[]) {
        String test = "0319-2133122";
        System.out.println(ValidUtil.isFixedPhone(test));

    }

}

转载于:https://www.cnblogs.com/murphyyy/p/10002753.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值