Commonly used regular verification tools

package com.codebase.util.validate;

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

/**
 * @describe:Commonly used regular validation
 * @author:houkai
 * @Date: 2018/4/4 11:07
 */
 public class ValidateUtil {  

    private static final String IPADDRESS_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

    /**
      * @Title: validateIpAddress
      * @Description: Verify if a string is an ip address
      * @param ipAddress
 * @return boolean return type
      */
 public static boolean validateIpAddress(String ipAddress) {
         if ( null ==ipAddress){
             return false ;         
        }
        Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
        Matches matches = pattern.matcher (ipAddress);
        return matcher.matches();
    }

    /**
      * @Title: validateNumber
      * @Description: Verify if a string is a number
      * @param number
 * @return boolean return type
      */
 public static boolean validateNumber(String number) {
         if ( null ==number){
             return false ;         
        }
        for (int i = 0; i < number.length(); i++) {
            if (!Character.isDigit(number.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 校验金额
     * @param sParamValue
*/
public static boolean validateMoney(String sParamValue){
        boolean result = true;         
        Pattern pattern = Pattern.compile("^\\d{1,26}\\.?\\d{0,4}$");
        Matcher isMoney = pattern.matcher (sParamValue);
        if( !isMoney.matches()){
            result = false;
        }
        return result;
    }

    /**
     * 校验利率
     * @param sParamValue
*/
public static boolean validateRate(String sParamValue){
        boolean result = true;         
        Pattern pattern = Pattern.compile("^\\d{1,5}\\.?\\d{0,4}$");
        Matcher isMoney = pattern.matcher (sParamValue);
        if( !isMoney.matches()){
            result = false;
        }
        return result;
    }

    /**
     * 校验价值
     * @param sParamValue
*/
public static boolean validateValue(String sParamValue){
        boolean result = true;         
        Pattern pattern = Pattern.compile("^\\d{1,18}\\.?\\d{0,2}$");
        Matcher isValue = pattern.matcher (sParamValue);
        if( !isValue.matches()){
            result = false;
        }
        return result;
    }

    /**
      * Validate email format
      */
 public static boolean validateEmail(String sParamValue) {
         boolean result = true ;    
        Pattern pattern = Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
        Matcher isEmail = pattern.matcher (sParamValue);
        if (!isEmail.matches()) {
            result = false;
        }
        return  result;
    }
    
    /**
      * Number of digits and decimals validation
      * Pass returns true, otherwise false
      */
 public static boolean validateNumber(String strParam, Integer length, Integer decimal) {
         boolean result = true ;    
        Integer integer = length - decimal;
        String regex = "^\\d{1," + integer + "}\\.?\\d{0," + decimal + "}$";
        Pattern pattern = Pattern.compile(regex);
        Matches matches = pattern.matcher (strParam);
        if (!matcher.matches()) {
            result = false;
        }
        return result;
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325442729&siteId=291194637