Skip to content
HangoverHangover
首页
项目收藏
留言板
github icon
  • 代码片段

      • 获取变量类型
        • 华氏度和摄氏度
          • 动态引入js
            • 判断类型

          判断类型

          author iconHangovercalendar icon2022年4月15日category icon
          • 判断
          tag icon
          • 判断
          • 正则
          timer icon小于 1 分钟

          判断类型

          const checkStr = (str, type) => {
              switch (type) {
                  case 'phone':   //手机号码
                      return /^1[3|4|5|6|7|8|9][0-9]{9}$/.test(str);
                  case 'tel':     //座机
                      return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
                  case 'card':    //身份证
                      return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
                  case 'pwd':     //密码以字母开头,长度在6~18之间,只能包含字母、数字和下划线
                      return /^[a-zA-Z]\w{5,17}$/.test(str)
                  case 'postal':  //邮政编码
                      return /[1-9]\d{5}(?!\d)/.test(str);
                  case 'QQ':      //QQ号
                      return /^[1-9][0-9]{4,9}$/.test(str);
                  case 'email':   //邮箱
                      return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
                  case 'money':   //金额(小数点2位)
                      return /^\d*(?:\.\d{0,2})?$/.test(str);
                  case 'URL':     //网址
                      return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str)
                  case 'IP':      //IP
                      return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str);
                  case 'date':    //日期时间
                      return /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(str) || /^(\d{4})\-(\d{2})\-(\d{2})$/.test(str)
                  case 'number':  //数字
                      return /^[0-9]$/.test(str);
                  case 'english': //英文
                      return /^[a-zA-Z]+$/.test(str);
                  case 'chinese': //中文
                      return /^[\\u4E00-\\u9FA5]+$/.test(str);
                  case 'lower':   //小写
                      return /^[a-z]+$/.test(str);
                  case 'upper':   //大写
                      return /^[A-Z]+$/.test(str);
                  case 'HTML':    //HTML标记
                      return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
                  default:
                      return true;
              }
          }
          
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
          37
          38
          39
          40
          上一页
          动态引入js
          鸟无声兮山寂寂,夜正长兮风淅淅。 ──李华《吊古战场文》
          Copyright © 2023 Hangover