前后台常用的一些数据规则验证(正则大集合)

原创 时光陀螺 随笔 实用类 156阅读 7 天前 举报

刚出道,给大家贴点实用的
常用的一些的规则验证
---这是java后台的
package cn.portalframe.base.util;

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

/**

  • 字符校验
  • 统一说明:返回值为true,表示输入合法,false:表示输入不合法
    */
    public class StringcharFilter {
    /**
    • 默认指定输入字符长度
      */
      private static final int strLength = 32;
      /**
    • 对输入字符串的长度进行校验
    • @param str
    • @param len 限制字符的长度,默认100
    • @return boolean
      */
      public static boolean Stringlength(String str,int len){
      if(StringUtils.isBlank(str.trim()))return false;
      if(str.trim().length()>(len >0 ? len :strLength))return false;
      return true;
      }

    /**

    • 验证输入特殊字符
    • @param str
    • @return boolean
      /
      public static boolean Special(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      // Matcher m = Pattern.compile("[\\^`~!@#$^&
      ()-=|{}':;',\[\].<>/?~!@#¥……&()——|{}【】\"‘;:”“'。,、?]+$").matcher(str);
      // return m.matches();
      String [] spStr = {"^","`","~","!","@","#","$","^","&","
      ","(",")","=","+","|","{","}","[",":",";","]","\\",".","<",">","¥","(",")","【","】","”","“","‘",";",":","。",",","、","?","——"};
      for(int i = 0;i<Arrays.asList(spStr).size();i++){
      if(str.indexOf(Arrays.asList(spStr).get(i)) != -1){
      return false;
      }
      }
      return true;
      }

    /**

    • 验证文本域的输入特殊字符
    • @param str
    • @return boolean
      /
      public static boolean CheckSpecial(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      String [] spStr = {"^","`","~","#","&","
      ","=","|","{","}","[","]","\\","¥","”","“","‘","—","+","</","<",">","/"};
      for(int i = 0;i<Arrays.asList(spStr).size();i++){
      if(str.indexOf(Arrays.asList(spStr).get(i)) != -1){
      return false;
      }
      }
      return true;
      }

    /**

    • 验证敏感字符
    • @param str
    • @return boolean
      */
      public static boolean Sensitive(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      String [] SenStr = {"location","<!--","->","<iframe","<form","<base","or","and","%27%3B","truncate","char","declare","javascript","<script>","script","</script>","vbscript>","onfocus","count","chr","mid","master","where","and","exec","null","alert","delete","select","insert","window","Script","eval","from","update","html","drop","alter"};
      for(int i = 0;i<Arrays.asList(SenStr).size();i++){
      if(str.toLowerCase().indexOf(Arrays.asList(SenStr).get(i)) != -1){
      return false;
      }
      }
      return true;
      }

    /**

    • 简单验证验证敏感字符
    • @param str
    • @return boolean
      */
      public static boolean easySensitive(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      String [] SenStr = {"location","<!--","->","<iframe","<form","<base","%27%3B","<",">","(",")","alert","javascript","<script>","script","</script>","vbscript>","eval","alert"};
      for(int i = 0;i<Arrays.asList(SenStr).size();i++){
      if(str.toLowerCase().indexOf(Arrays.asList(SenStr).get(i)) != -1){
      return false;
      }
      }
      return true;
      }
      /**
    • 验证关键字
    • @param str
    • @return boolean
      */
      public static boolean CheckFileName(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      String [] SenStr = {"javascript","script","jscript","alert","location","onclick","eval","onload","load"};
      for(int i = 0;i<Arrays.asList(SenStr).size();i++){
      if(str.toLowerCase().indexOf(Arrays.asList(SenStr).get(i)) != -1){
      return false;
      }
      }
      return true;
      }
      /**
    • 验证特殊字符
    • @param str
    • @return boolean
      /
      public static boolean CheckFileNameStr(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      String [] SenStr = {"`","~","!","@","#","\\","
      ","(",")","<",">","[","]"};
      for(int i = 0;i<Arrays.asList(SenStr).size();i++){
      if(str.toLowerCase().indexOf(Arrays.asList(SenStr).get(i)) != -1){
      return false;
      }
      }
      return true;
      }

    /**

    • 验证只允许输入汉字
    • @param str
    • @return boolean
      */
      public static boolean Chinchar(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("^[\u4e00-\u9fa5]{0,}$").matcher(str);
      return m.matches();
      }

    /**

    • 验证只允许输入英文字母,不区分大小写
    • @param str
    • @return boolean
      */
      public static boolean English(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("^[A-Za-z]+$").matcher(str);
      return m.matches();
      }

    /**

    • 验证只能输入数字
    • @param str
    • @return boolean
      /
      public static boolean Number(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("^[0-9]
      $").matcher(str);
      return m.matches();
      }

    /**

    • 验证输入只能包含数字和英文字母
    • @param str
    • @return boolean
      */
      public static boolean NumEng(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("^[A-Za-z0-9]+$").matcher(str);
      return m.matches();
      }

    /**

    • 验证输入只能包含汉字,数字,英文字母
    • @param str
    • @return boolean
      */
      public static boolean NumEngChar(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("^[\u4E00-\u9FA5A-Za-z0-9]+$").matcher(str);
      return m.matches();
      }

    /**

    • 验证邮政编码是否合法
    • @param str
    • @return boolean
      */
      public static boolean Postaicode(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("[1-9]\\d{5}(?!\\d)").matcher(str);
      return m.matches();
      }

    /**

    • 验证URL是否合法
    • @param str
    • @return boolean
      /
      public static boolean IP(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("((http|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})
      (/[a-zA-Z0-9\\&%_\./-~-]*)?").matcher(str);
      return m.matches();
      }

    /**

    • 验证邮箱是否合法
    • @param str
    • @return boolean
      /
      public static boolean Email(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("^\\w+([-+.]\\w+)
      @\\w+([-.]\\w+)\.\\w+([-.]\\w+)$").matcher(str);
      return m.matches();
      }
      /**
    • 验证QQ号码是否合法
    • @param str
    • @return boolean
      */
      public static boolean QQ(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("[1-9][0-9]{4,}").matcher(str);
      return m.matches();
      }

    /**

    • 验证身份证是否合法
    • @param str
    • @return boolean
      */
      public static boolean IDcard(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)").matcher(str);
      return m.matches();
      }

    /**

    • 验证手机号输入是否合法
    • @param str
    • @return boolean
      */
      public static boolean Telephone(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("^1[3|4|5|8][0-9]\\d{8}$").matcher(str);
      return m.matches();
      }

    /**

    • 验证电话号码是否合法
    • 支持手机号码,3-4位区号,7-8位直播号码,1-4位分机号
    • @param str
    • @return boolean
      */
      public static boolean Phone(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)").matcher(str);
      return m.matches();
      }

    /**

    • 验证域名是否合法
    • @param str
    • @return boolean
      */
      public static boolean RealmName(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?").matcher(str);
      return m.matches();
      }

    /**

    • 输入中可以包含<>._!@:/?,();;:,。、?()【】等字符,默认合法
    • @param str
    • @return boolean
      */
      public static boolean NumEngCharSpecialOK(String str){
      if(StringUtils.isBlank(str.trim()))return false;
      Matcher m = Pattern.compile("^[\u4E00-\u9FA5A-Za-z0-9<>[-]._!@:/?,();;:,$。!、?()【】]+$").matcher(str);
      return m.matches();
      }

      /**

      • 验证银行卡号是否合法
      • 校验过程:
      • 1、从卡号最后一位数字开始,逆向将奇数位(1、3、5等等)相加。
      • 2、从卡号最后一位数字开始,逆向将偶数位数字,先乘以2(如果乘积为两位数,将个位十位数字相加,即将其减去9),再求和。
      • 3、将奇数位总和加上偶数位总和,结果可以被10整除。
      • @param bankCard
      • @return boolean
        */
        public static boolean checkBankCard(String bankCard) {
        if(bankCard.length() < 15 || bankCard.length() > 19) {
        return false;
        }
        char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1));
        if(bit == 'N'){
        return false;
        }
        return bankCard.charAt(bankCard.length() - 1) == bit;
        }

    /**

    • 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
    • @param nonCheckCodeBankCard
    • @return char
      /
      private static char getBankCardCheckCode(String nonCheckCodeBankCard){
      if(nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0|| !nonCheckCodeBankCard.matches("\\d+")) {
      return 'N';//如果传的不是数据返回N
      }
      char[] chs = nonCheckCodeBankCard.trim().toCharArray();
      int luhmSum = 0;
      for(int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
      int k = chs[i] - '0';
      if(j % 2 == 0) {
      k
      = 2;
      k = k / 10 + k % 10;
      }
      luhmSum += k;
      }
      return (luhmSum % 10 == 0) ? '0' : (char)((10 - luhmSum % 10) + '0');
      }

    /**

    • 过滤文本中的html标签
    • @param inputString
    • @return String
      /
      public static String FilterHtml(String htmlStr) {
      if (StringUtils.isEmpty(htmlStr)) return null;
      String regEx_script = "<[\\s]
      ?script[^>]?>[\\s\\S]?<[\\s]?\\/[\\s]?script[\\s]?>"; // 定义script的正则表达式{或<script[^>]?>[\\s\\S]?<\\/script> // }
      String regEx_style = "<[\\s]
      ?style[^>]?>[\\s\\S]?<[\\s]?\\/[\\s]?style[\\s]?>"; // 定义style的正则表达式{或<style[^>]?>[\\s\\S]?<\\/style>// }
      String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
      String regEx_html1 = "\\s
      |\t|\r|\n";//定义空格回车换行符号
      htmlStr = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE).matcher(htmlStr).replaceAll(""); // 过滤script标签
      htmlStr = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE).matcher(htmlStr).replaceAll(""); // 过滤style标签
      htmlStr = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE).matcher(htmlStr).replaceAll(""); // 过滤html标签
      htmlStr = Pattern.compile(regEx_html1, Pattern.CASE_INSENSITIVE).matcher(htmlStr).replaceAll(""); //定义空格回车换行符号
      // 替换&amp;nbsp;
      htmlStr = htmlStr.replaceAll("&amp;", "").replaceAll("nbsp;", "");
      return htmlStr;// 返回文本字符串
      }

}

---这是相对应的js前台的
/**

  • 添加验证的工具类
  • 调用方式:函数式调用
    /
    Function.prototype.addMethod = function(name,fn){
    this[name]=fn;
    return this;
    }
    var Methods = function(){};
    Methods.addMethod('getDateDiff', function(timeStamp){//将当前的时间戳格式化为多长时间之前的格式--param:时间戳;return:String
    if(!$.trim(timeStamp)) return null;
    var nowTime = new Date().getTime(),minutes = 1000
    60,hours = minutes60,day=hours24,weekC = day7,month=day30;//获取需要的时间
    var difference = nowTime-timeStamp;
    if(difference < 0)return;
    if(difference/month>1)return ""+parseInt(difference/month)+"个月前";
    if(difference/weekC>1)return ""+parseInt(difference/weekC)+"周前";
    if(difference/day>1)return ""+parseInt(difference/day)+"天前";
    if(difference/hours>1)return ""+parseInt(difference/hours)+"小时前";
    if(difference/minutes>1)return ""+parseInt(difference/minutes)+"分钟前";
    return "刚刚";

}).addMethod('checkStr',function(str){//特殊字符过滤;param:String;return:String;
if(!$.trim(str))return null;
var rs="",pattern = new RegExp("[~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]"); for (var i = 0; i < str.length; i++) { var b=pattern.test(str.substr(i, 1)); if(b)rs = rs+' '+str.substr(i, 1); } return rs; }).addMethod('cutStr',function(str,len){//字符串截取省略号显示,param:String;return:String; if(!$.trim(str))return null; if(!$.trim(len)) len = 10;//默认截取长度10; if($.trim(str).length<len){ return str; } return str.substr(0,len)+'...'; }).addMethod('sensitiveChar',function(str){//敏感字符过滤,param:String,return:boolean;验证合法:返回true if(!$.trim(str))return false; var pattern=new RegExp("[~!@#\$%\*()+<>]"),keywordCode = /javascript|script|jscript|alert|location|onclick|eval/g;
if(pattern.test(str) || keywordCode.test(str)) return false;
return true;
}).addMethod('IDcard',function(str){//身份证校验;param:str;return:boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)");
return pattern.test(str);
}).addMethod('Telephone',function(str){//验证手机号码是否合法;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("^1[3|4|5|8][0-9]\\d{8}$");
return pattern.test(str);
}).addMethod('Phone',function(str){//验证电话号码是否合法param:str;return:boolean;验证合法:返回true; 支持手机号码,3-4位区号,7-8位直播号码,1-4位分机号
if(!$.trim(str))return false;
var pattern=new RegExp("((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)");
return pattern.test(str);
}).addMethod('RealmName', function(str){//验证yuming是否合法;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?");
return pattern.test(str);
}).addMethod('QQ', function(str){//验证QQ号码是否合法;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("[1-9][0-9]{4,}");
return pattern.test(str);
}).addMethod('Email', function(str){//验证邮箱是否合法;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("^\\w+([-+.]\\w+)@\\w+([-.]\\w+)\.\\w+([-.]\\w+)$");
return pattern.test(str);
}).addMethod('IP', function(str){//验证IP是否合法;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("((http|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})
(/[a-zA-Z0-9\\&%_\./-~-])?");
return pattern.test(str);
}).addMethod('Postaicode', function(str){//验证邮政编码是否合法;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("[1-9]\\d{5}(?!\\d)");
return pattern.test(str);
}).addMethod('NumEngChar', function(str){//验证输入只能包含汉字,数字,英文字母;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("^[\u4E00-\u9FA5A-Za-z0-9]+$");
return pattern.test(str);
}).addMethod('NumEng', function(str){//验证输入只能包含数字和英文字母;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("^[A-Za-z0-9]+$");
return pattern.test(str);
}).addMethod('Number', function(str){//验证只能输入数字;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("^[0-9]
$");
return pattern.test(str);
}).addMethod('English', function(str){// 验证只允许输入英文字母,不区分大小写;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("^[A-Za-z]+$");
return pattern.test(str);
}).addMethod('Chinchar', function(str){//验证只允许输入汉字;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
var pattern=new RegExp("^[\u4e00-\u9fa5]{0,}$");
return pattern.test(str);
}).addMethod('strLength', function(str,len){//验证字符串的输入长度,默认值100;param:String;return :boolean;验证合法:返回true
if(!$.trim(str))return false;
if(!$.trim(len))len=100;
if($.trim(str).length<len)return true;
return false;
}).addMethod('transDayTimefn',function(time){//时间格式化,格式化为时间的标准格式yyyy-mm-dd hh:ss:nn
if(!$.trim(time))return false;
var d= new Date(time),year = new String(d.getFullYear()), month = new String((d.getMonth()+1)),day = new String(d.getDate()), hours = new String(d.getHours()), minutes= new String(d.getMinutes()),seconds = new String(d.getSeconds());
month= month.length==1?"0"+month:month;
day= day.length==1?"0"+day:day;
hours= hours.length==1?"0"+hours:hours;
minutes= minutes.length==1?"0"+minutes:minutes;
seconds= seconds.length==1?"0"+seconds:seconds;
return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
});

评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复