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

    }

}
 posted on 2016-10-09 14:11  杨成雨  阅读(156)  评论(0编辑  收藏  举报