首页 > 编程语言 > 详细

javascript-typeof篇

时间:2018-03-31 23:37:06      阅读:223      评论:0      收藏:0      [点我收藏+]
  1 isString (o) { //是否字符串
  2     return Object.prototype.toString.call(o).slice(8, -1) === ‘String‘
  3 }
  4 
  5 isNumber (o) { //是否数字
  6     return Object.prototype.toString.call(o).slice(8, -1) === ‘Number‘
  7 }
  8 
  9 isObj (o) { //是否对象
 10     return Object.prototype.toString.call(o).slice(8, -1) === ‘Object‘
 11 }
 12 
 13 isArray (o) { //是否数组
 14     return Object.prototype.toString.call(o).slice(8, -1) === ‘Array‘
 15 }
 16 
 17 isDate (o) { //是否时间
 18     return Object.prototype.toString.call(o).slice(8, -1) === ‘Date‘
 19 }
 20 
 21 isBoolean (o) { //是否boolean
 22     return Object.prototype.toString.call(o).slice(8, -1) === ‘Boolean‘
 23 }
 24 
 25 isFunction (o) { //是否函数
 26     return Object.prototype.toString.call(o).slice(8, -1) === ‘Function‘
 27 }
 28 
 29 isNull (o) { //是否为null
 30     return Object.prototype.toString.call(o).slice(8, -1) === ‘Null‘
 31 }
 32 
 33 isUndefined (o) { //是否undefined
 34     return Object.prototype.toString.call(o).slice(8, -1) === ‘Undefined‘
 35 }
 36 
 37 isFalse (o) {
 38     if (o == ‘‘ || o == undefined || o == null || o == ‘null‘ || o == ‘undefined‘ || o == 0 || o == false || o == NaN) return true
 39     return false
 40 }
 41 
 42 isTrue (o) {
 43     return !this.isFalse(o)
 44 }
 45 
 46 isIos () {
 47     var u = navigator.userAgent;
 48     if (u.indexOf(‘Android‘) > -1 || u.indexOf(‘Linux‘) > -1) {//安卓手机
 49         // return "Android";
 50         return false
 51     } else if (u.indexOf(‘iPhone‘) > -1) {//苹果手机
 52         // return "iPhone";
 53         return true
 54     } else if (u.indexOf(‘iPad‘) > -1) {//iPad
 55         // return "iPad";
 56         return false
 57     } else if (u.indexOf(‘Windows Phone‘) > -1) {//winphone手机
 58         // return "Windows Phone";
 59         return false
 60     }else{
 61         return false
 62     }
 63 }
 64 
 65 isPC () { //是否为PC端
 66     var userAgentInfo = navigator.userAgent;
 67     var Agents = ["Android", "iPhone",
 68                 "SymbianOS", "Windows Phone",
 69                 "iPad", "iPod"];
 70     var flag = true;
 71     for (var v = 0; v < Agents.length; v++) {
 72         if (userAgentInfo.indexOf(Agents[v]) > 0) {
 73             flag = false;
 74             break;
 75         }
 76     }
 77     return flag;
 78 }
 79 
 80 browserType(){
 81     var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
 82     var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
 83     var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
 84     var isEdge = userAgent.indexOf("Edge") > -1; //判断是否IE的Edge浏览器
 85     var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
 86     var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器
 87     var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器
 88     if (isIE) {
 89         var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
 90         reIE.test(userAgent);
 91         var fIEVersion = parseFloat(RegExp["$1"]);
 92         if(fIEVersion == 7) return "IE7"
 93         else if(fIEVersion == 8) return "IE8";
 94         else if(fIEVersion == 9) return "IE9";
 95         else if(fIEVersion == 10) return "IE10";
 96         else if(fIEVersion == 11) return "IE11";
 97         else return "IE7以下"//IE版本过低
 98     }
 99 
100     if (isFF) return "FF";
101     if (isOpera) return "Opera";
102     if (isEdge) return "Edge";
103     if (isSafari) return "Safari";
104     if (isChrome) return "Chrome";
105 }
106 
107 checkStr (str, type) {
108     switch (type) {
109         case ‘phone‘:   //手机号码
110             return /^1[3|4|5|6|7|8][0-9]{9}$/.test(str);
111         case ‘tel‘:     //座机
112             return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
113         case ‘card‘:    //身份证
114             return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
115         case ‘pwd‘:     //密码以字母开头,长度在6~18之间,只能包含字母、数字和下划线
116             return /^[a-zA-Z]\w{5,17}$/.test(str)
117         case ‘postal‘:  //邮政编码
118             return /[1-9]\d{5}(?!\d)/.test(str);
119         case ‘QQ‘:      //QQ号
120             return /^[1-9][0-9]{4,9}$/.test(str);
121         case ‘email‘:   //邮箱
122             return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
123         case ‘money‘:   //金额(小数点2位)
124             return /^\d*(?:\.\d{0,2})?$/.test(str);
125         case ‘URL‘:     //网址
126             return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str)
127         case ‘IP‘:      //IP
128             return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str);
129         case ‘date‘:    //日期时间
130             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)
131         case ‘number‘:  //数字
132             return /^[0-9]$/.test(str);
133         case ‘english‘: //英文
134             return /^[a-zA-Z]+$/.test(str);
135         case ‘chinese‘: //中文
136             return /^[\u4E00-\u9FA5]+$/.test(str);
137         case ‘lower‘:   //小写
138             return /^[a-z]+$/.test(str);
139         case ‘upper‘:   //大写
140             return /^[A-Z]+$/.test(str);
141         case ‘HTML‘:    //HTML标记
142             return /<("[^"]*"|‘[^‘]*‘|[^‘">])*>/.test(str);
143         default:
144             return true;
145     }
146 }
147 
148 // 严格的身份证校验
149 isCardID(sId) {
150     if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) {
151         alert(‘你输入的身份证长度或格式错误‘)
152         return false
153     }
154     //身份证城市
155     var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
156     if(!aCity[parseInt(sId.substr(0,2))]) { 
157         alert(‘你的身份证地区非法‘)
158         return false
159     }
160 
161     // 出生日期验证
162     var sBirthday=(sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2))).replace(/-/g,"/"),
163         d = new Date(sBirthday)
164     if(sBirthday != (d.getFullYear()+"/"+ (d.getMonth()+1) + "/" + d.getDate())) {
165         alert(‘身份证上的出生日期非法‘)
166         return false
167     }
168 
169     // 身份证号码校验
170     var sum = 0,
171         weights =  [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
172         codes = "10X98765432"
173     for (var i = 0; i < sId.length - 1; i++) {
174         sum += sId[i] * weights[i];
175     }
176     var last = codes[sum % 11]; //计算出来的最后一位身份证号码
177     if (sId[sId.length-1] != last) { 
178         alert(‘你输入的身份证号非法‘)
179         return false
180     }
181 
182     return true
183 }
184 }

 

javascript-typeof篇

原文:https://www.cnblogs.com/studyshufei/p/8684591.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!