1.验证邮箱
function isEmail (number){
return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(number);
}
2.验证手机号码
function isMobile (number) {
return /^1[0-9]{10}$/.test(number)
}
3.验证是否url地址
function isURL(number){
return /^http[s]?:\/\/.*/.test(number)
}
4.验证是否字符串
function isString (s){
return Object.prototype.toString.call(s).slice(8, -1) === 'String'
}
5.验证是否数字
function isNumber (s){
return Object.prototype.toString.call(s).slice(8, -1) === 'Number'
}
6.验证是否bool
function isBoolean (s){
return Object.prototype.toString.call(s).slice(8, -1) === 'Boolean'
}
7.验证是否函数
function isFunction (s){
return Object.prototype.toString.call(s).slice(8, -1) === 'Function'
}
8.验证是否对象
function isObject (s){
return Object.prototype.toString.call(s).slice(8, -1) === 'Object'
}
9.验证是否数组
function isArray (s){
return Object.prototype.toString.call(s).slice(8, -1) === 'Array'
}
10.验证是否正则
function isRegExp(s){
return Object.prototype.toString.call(s).slice(8, -1) === 'RegExp'
}
11.验证是否错误对象
function isError (s){
return Object.prototype.toString.call(s).slice(8, -1) === 'Error'
}
12.验证是否微信浏览器
function isWeiXin (){
return ua.match(/microMessenger/i) == 'micromessenger'
}
13.验证是否移动端
function isDeviceMobile (){
return /android|webos|iphone|ipod|balckberry/i.test(ua)
}
14.验证是否QQ浏览器
function isQQBrowser (){
return !!ua.match(/mqqbrowser|qzone|qqbrowser|qbwebviewtype/i)
}
15.验证是否iOS
function isIos (){
var u = navigator.userAgent;
if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) { //安卓手机
return false
} else if (u.indexOf('iPhone') > -1) {//苹果手机
return true
} else if (u.indexOf('iPad') > -1) {//iPad
return false
} else if (u.indexOf('Windows Phone') > -1) {//winphone手机
return false
} else {
return false
}
}
16.验证是否PC端
function isPC (){
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
17.去除HTML标签
function removeHtmltag (str){
return str.replace(/<[^>]+>/g, '')
}
18.获取url参数
function getQueryString (str){
const reg = new RegExp('(^|&)' + str + '=([^&]*)(&|$)', 'i');
const search = window.location.search.split('?')[1] || '';
const r = search.match(reg) || [];
return r[2];
}
19.根据url地址下载
function download (url){
var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
var isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
if (isChrome || isSafari) {
var link = document.createElement('a');
link.href = url;
if (link.download !== undefined) {
var fileName = url.substring(url.lastIndexOf('/') + 1, url.length);
link.download = fileName;
}
if (document.createEvent) {
var e = document.createEvent('MouseEvents');
e.initEvent('click', true, true);
link.dispatchEvent(e);
return true;
}
}
if (url.indexOf('?') === -1) {
url += '?download';
}
window.open(url, '_self');
return true;
}
20.动态引入js
function injectScript (src){
const s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = src;
const t = document.getElementsByTagName('script')[0];
t.parentNode.insertBefore(s, t);
}
21.是否包含某个class
function hasClass (el, className){
let reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
return reg.test(el.className)
}
22.el添加某个class
function addClass (el, className){
if (hasClass(el, className)) {
return
}
let newClass = el.className.split(' ')
newClass.push(className)
el.className = newClass.join(' ')
}
23.el去除某个class
function removeClass(el, className){
if (!hasClass(el, className)) {
return
}
let reg = new RegExp('(^|\\s)' + className + '(\\s|$)', 'g')
el.className = el.className.replace(reg, ' ')
}
24.滚动到顶部
function scrollToTop (){
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
}
25.判断一个元素是否在数组中
function contains (arr, val){
return arr.indexOf(val) != -1 ? true : false;
}
26.去重
function unique (arr){
if(!Array.isArray(arr)){
return;
}
return Array.form(new Set(arr))
}
27.删除其中一个元素
function remove (arr, ele){
var index = arr.indexOf(ele);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
28.最大值
function max (arr){
return Math.max.apply(null, arr);
}
29.最小值
function min (arr){
return Math.min.apply(null, arr);
}
30.去除空格,type: 1-所有空格 2-前后空格 3-前空格 4-后空格
function trim (str, type){
type = type || 1
switch (type) {
case 1:
return str.replace(/\s+/g, "");
case 2:
return str.replace(/(^\s*)|(\s*$)/g, "");
case 3:
return str.replace(/(^\s*)/g, "");
case 4:
return str.replace(/(\s*$)/g, "");
default:
return str;
}
}
31.字符转换,type: 1:首字母大写 2:首字母小写 3:大小写转换 4:全部大写 5:全部小写
function changeCase (str, type){
type = type || 4
switch (type) {
case 1:
return str.replace(/\b\w+\b/g, function (word) {
return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
});
case 2:
return str.replace(/\b\w+\b/g, function (word) {
return word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase();
});
case 3:
return str.split('').map(function (word) {
if (/[a-z]/.test(word)) {
return word.toUpperCase();
} else {
return word.toLowerCase()
}
}).join('')
case 4:
return str.toUpperCase();
case 5:
return str.toLowerCase();
default:
return str;
}
}
32.检验密码强度
function checkPwd (str){
var Lv = 0;
if (str.length < 6) {
return Lv
}
if (/[0-9]/.test(str)) {
Lv++
}
if (/[a-z]/.test(str)) {
Lv++
}
if (/[A-Z]/.test(str)) {
Lv++
}
if (/[\.|-|_]/.test(str)) {
Lv++
}
return Lv;
}
33.在字符串中插入新字符串
function insertStr (soure, index, newStr){
var str = soure.slice(0, index) + newStr + soure.slice(index);
return str;
}
34.追加url参数
function appendQuery (url, key, value){
var options = key;
if (typeof options == 'string') {
options = {};
options[key] = value;
}
options = $.param(options);
if (url.includes('?')) {
url += '&' + options
} else {
url += '?' + options
}
return url;
}