⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validator.java

📁 java版源代码,里面包含很多源代码,大家可以看看.
💻 JAVA
字号:
package com.trulytech.mantis.util;

import javax.servlet.http.HttpServletRequest;
import com.trulytech.mantis.system.Properties;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 *
 * <p>Title: Mantis</p>
 *
 * <p>Description: 后台HTTP校验类</p>
 *
 * <p>Copyright: Copyright (c) 2002</p>
 *
 * <p>Company: </p>
 *
 * @author Wang Xian
 * @version 1.0
 */

public class Validator {

  /**
   * 参数是否为空
   * @param request HttpServletRequest
   * @param Name String 参数名
   * @return boolean 是否为空
   */
  public static boolean isEmpty(HttpServletRequest request, String Name) {
    String Param = getParameter(request, Name);
    if (Param.length() == 0)
      return true;
    else
      return false;
  }

  /**
   * 参数是否是字符串
   * @param request HttpServletRequest
   * @param Name String 参数名
   * @param len int 字符串长度
   * @param required boolean 是否必须录入
   * @return boolean 是否合法
   * @throws UnsupportedEncodingException
   */
  public static boolean isString(HttpServletRequest request, String Name,
                                 int len,
                                 boolean required) throws java.io.
      UnsupportedEncodingException {
    String Param = getParameter(request, Name);
    if (!required && Param.length() == 0)
      return true;
    else if (required && Param.length() == 0)
      return false;
    else {
      int length = getParameterLength(request, Name);

      if (length <= len)
        return true;
      else
        return false;
    }
  }

  /**
   * 是否是Integer类型
   * @param request HttpServletRequest
   * @param Name String 参数名
   * @param maxValue int 最大值
   * @param minValue int 最小值
   * @param required boolean 是否必须录入
   * @return boolean 是否合法
   * @throws UnsupportedEncodingException
   */
  public static boolean isInteger(HttpServletRequest request, String Name,
                                  int maxValue, int minValue,
                                  boolean required) throws java.io.
      UnsupportedEncodingException {

    String Param = getParameter(request, Name);
    if (!required && Param.length() == 0)
      return true;
    else if (required && Param.length() == 0)
      return false;
    else {
      Integer value = null;
      try {
        value = Integer.valueOf(getParameter(request, Name));
      }
      catch (NumberFormatException e) {
        return false;
      }
      if (value.intValue() >= minValue && value.intValue() <= maxValue)
        return true;
      else
        return false;
    }

  }

  /**
   * 是否是Long类型
   * @param request HttpServletRequest
   * @param Name String
   * @param maxValue long
   * @param minValue long
   * @param required boolean
   * @return boolean
   * @throws UnsupportedEncodingException
   */
  public static boolean isLong(HttpServletRequest request, String Name,
                               long maxValue, long minValue,
                               boolean required) throws java.io.
      UnsupportedEncodingException {

    String Param = getParameter(request, Name);
    if (!required && Param.length() == 0)
      return true;
    else if (required && Param.length() == 0)
      return false;
    else {
      Long value = null;
      try {
        value = Long.valueOf(getParameter(request, Name));
      }
      catch (NumberFormatException e) {
        return false;
      }
      if (value.longValue() >= minValue && value.longValue() <= maxValue)
        return true;
      else
        return false;
    }

  }

  /**
   * 是否是Float类型
   * @param request HttpServletRequest
   * @param Name String
   * @param maxValue float
   * @param minValue float
   * @param required boolean
   * @return boolean
   * @throws UnsupportedEncodingException
   */
  public static boolean isFloat(HttpServletRequest request, String Name,
                                float maxValue, float minValue,
                                boolean required) throws java.io.
      UnsupportedEncodingException {

    String Param = getParameter(request, Name);
    if (!required && Param.length() == 0)
      return true;
    else if (required && Param.length() == 0)
      return false;
    else {
      Float value = null;
      try {
        value = Float.valueOf(getParameter(request, Name));
      }
      catch (NumberFormatException e) {
        return false;
      }
      if (value.floatValue() >= minValue && value.floatValue() <= maxValue)
        return true;
      else
        return false;
    }

  }

  /**
   * 是否是Double类型
   * @param request HttpServletRequest
   * @param Name String
   * @param maxValue double
   * @param minValue double
   * @param required boolean
   * @return boolean
   * @throws UnsupportedEncodingException
   */
  public static boolean isDouble(HttpServletRequest request, String Name,
                                 double maxValue, double minValue,
                                 boolean required) throws java.io.
      UnsupportedEncodingException {

    String Param = getParameter(request, Name);
    if (!required && Param.length() == 0)
      return true;
    else if (required && Param.length() == 0)
      return false;
    else {
      Double value = null;
      try {
        value = Double.valueOf(getParameter(request, Name));
      }
      catch (NumberFormatException e) {
        return false;
      }
      if (value.doubleValue() >= minValue && value.doubleValue() <= maxValue)
        return true;
      else
        return false;
    }

  }

  /**
   * 正则表达式校验
   * @param request HttpServletRequest
   * @param Name String 参数名
   * @param Regx String 正则表达式  <BR>
   * "^\\d+$"  //非负整数(正整数 + 0)  <BR>
   * "^[0-9]*[1-9][0-9]*$"  //正整数  <BR>
   * "^((-\\d+)|(0+))$"  //非正整数(负整数 + 0)  <BR>
   * "^-[0-9]*[1-9][0-9]*$"  //负整数  <BR>
   * "^-?\\d+$"    //整数  <BR>
   * "^\\d+(\\.\\d+)?$"  //非负浮点数(正浮点数 + 0)  <BR>
   * "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数  <BR>
   * "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"  //非正浮点数(负浮点数 + 0)  <BR>
   * "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数  <BR>
   * "^(-?\\d+)(\\.\\d+)?$"  //浮点数  <BR>
   * "^[A-Za-z]+$"  //由26个英文字母组成的字符串  <BR>
   * "^[A-Z]+$"  //由26个英文字母的大写组成的字符串  <BR>
   * "^[a-z]+$"  //由26个英文字母的小写组成的字符串  <BR>
   * "^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串  <BR>
   * "^\\w+$"  //由数字、26个英文字母或者下划线组成的字符串  <BR>
   * "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"    //email地址  <BR>
   * "^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"  //url  <BR>
   * @param required boolean 是否必录
   * @return boolean 是否合法
   * @throws UnsupportedEncodingException
   */
  public static boolean isRegxValid(HttpServletRequest request, String Name,
                                    String Regx,
                                    boolean required) throws java.io.
      UnsupportedEncodingException {
    String Param = getParameter(request, Name);
    if (!required && Param.length() == 0)
      return true;
    else if (required && Param.length() == 0)
      return false;
    else {

      String Value = getParameter(request, Name);

      Pattern p = Pattern.compile(Regx);
      Matcher m = p.matcher(Value);
      if (m.find())
        return true;
      else
        return false;
    }

  }

  /**
   * 获得参数长度 (Byte)
   * @param request HttpServletRequest
   * @param Name String
   * @return int
   * @throws UnsupportedEncodingException
   */
  protected static int getParameterLength(HttpServletRequest request,
                                          String Name) throws
      java.io.UnsupportedEncodingException {
    String tmpStr = request.getParameter(Name);
    if (tmpStr == null)
      return 0;
    if (Properties.isConvert && Properties.isInternational)
      return Properties.ConvertCharset(tmpStr).getBytes("UTF-8").length;
    else if (Properties.isConvert && !Properties.isInternational)
      return Properties.ConvertCharset(tmpStr).getBytes("GBK").length;
    else
      return tmpStr.getBytes().length;
  }

  /**
   * 获得参数值
   * @param request HttpServletRequest
   * @param Name String
   * @return String
   */
  protected static String getParameter(HttpServletRequest request, String Name) {
    String tmpStr = request.getParameter(Name);
    if (tmpStr == null)
      tmpStr = "";
    if (Properties.isConvert)
      return Properties.ConvertCharset(tmpStr);
    else
      return tmpStr;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -