utils.js檔案;一些常用方法的備份

來源:互聯網
上載者:User

標籤:status   訪問   sub   方法   號碼   src   驗證   var   mob   

一些常用方法備份:

function _(value) {    value = ‘0‘ + value;    return value.substr(value.length - 2);}Date.prototype.format = function(split) {    split = split || "";    if(split === ‘time‘) {        return _(this.getHours()) + ‘:‘ + _(this.getMinutes()) + ‘:‘ + _(this.getSeconds()); /*返回  時:分:秒*/    } else {        var month = this.getMonth() + 1;        month = month < 10 ? "0" + month : month;        var day = this.getDate();        day = day < 10 ? "0" + day : day;        return this.getFullYear() + split + month + split + day; /*返回  年月日*/    }}Date.prototype.time = function() {    var hour = this.getHours();    var minute = this.getMinutes();    hour = hour < 10 ? "0" + hour : hour;    minute = minute < 10 ? "0" + minute : minute;    return hour + ":" + minute; /*返回  時:分*/}Date.prototype.addDays = function(n) {    return new Date(this.setDate(this.getDate() + n));};Date.prototype.addMonths = function(n) {    return new Date(this.setMonth(this.getMonth() + n));};Array.prototype.max = function() {    return Math.max.apply(null, this);}Array.prototype.min = function() {    return Math.min.apply(null, this);}Array.prototype.maxVal = function() {    return this.sort()[this.length - 1];}Array.prototype.minVal = function() {    return this.sort()[0];}Array.prototype.update = function(value) {    var pos = $.inArray(value, this);    if(pos === -1) {        this.push(value);    } else {        this.splice(pos, 1);    }    return this;}String.prototype.format = function(split) {    split = split || ‘‘;    var value;    if(this.indexOf(‘/‘) !== -1) {        value = this.replace(/\//g, split);    } else if(this.indexOf(‘-‘) !== -1) {        value = this.replace(/-/g, split);    } else {        value = this.substr(0, 4) + split + this.substr(4, 2) + split + this.substr(6);    }    if(split === ‘date‘) {        var arr = value.split(split);        value = new Date(arr[0], parseInt(arr[1]) - 1, arr[2])    }    if(split === ‘timeStamp‘) {        var arr = value.split(split);        value = new Date(arr[0], parseInt(arr[1]) - 1, arr[2])        value = value.getTime();    }    return value;}String.prototype.decimal = function() {    var value;    if(this.indexOf(‘.‘) == -1) {        value = this + ‘.00‘;    } else if(this.split(".")[1].length == 1) {        value = this + ‘0‘;    } else {        value = this + ‘‘;    }    return value;}var A = {    Service: {        get: function(url, data, success, error) {            $.ajax({                url: url,                type: "GET",                dataType: "json",                data: data || "",                cache: false,                success: function(result) {                    success && success(result);                },                error: function(xhr) {                    var index = layer.alert(xhr.responseJSON[0].content, {                        title: ‘提示‘                    }, function() {                        if(xhr.status == 401) {                            layer.close(index);                            //調取父級頁面的退出函數                            self.parent.window.quit();                        } else {                            layer.close(index);                        }                    });                    error && error();                }            })        },        post: function(url, data, success, error) {            data = JSON.stringify(data);            $.ajax({                url: url,                type: "POST",                dataType: "json",                data: data,                cache: false,                contentType: "application/json",                success: function(result) {                    success && success(result);                },                error: function(xhr) {                    if(xhr.status == 401) {                        var index = layer.alert(xhr.responseJSON[0].content, {                            title: ‘提示‘                        }, function() {                            layer.close(index);                            self.parent.window.quit();                        });                    } else if(xhr.status == 404) {                        var index = layer.alert(‘訪問錯誤或內部伺服器錯誤‘, {                            title: ‘提示‘                        }, function() {                            layer.close(index);                        });                    } else {                        layer.msg(xhr.responseJSON[0].content, {                            time: 2000                        });                    }                    error && error();                }            })        },        jsonp: function(url, data, callback) {            $.ajax({                url: url,                dataType: "jsonp",                data: data,                jsonp: "callback",                success: function(result) {                    callback(result);                },                timeout: 3000            });        }    },    params: function() {        var params = decodeURIComponent(window.location.search).substr(1);        if(params.indexOf(‘=‘) !== -1) {            var arr = params.split(‘&‘);            var json = {};            for(var i = 0; i < arr.length; i++) {                var t = arr[i].split(‘=‘);                json[t[0]] = t[1];            }            return json;        } else {            return params;        }    },    hash: function() {        var hash = decodeURIComponent(window.location.hash).substr(1);        var arr = hash.split(‘-‘);        return arr[1];    },    //字串//擷取//最後一位    gerStr: function(str) {        var s = ‘‘;        s = str.charAt(str.length - 1);        return s;    },    //字串//去除//最後一位    cutStr: function(str) {        var s = ‘‘;        s = str.substring(0, str.length - 1);        return s;    },    express: {        mobile: {            exp: /^[1][3-8]\d{9}$/,            message: "請輸入正確的手機號碼"        },        password: {            exp: /^[[email protected]]+$/,            message: "密碼由字母、數字、底線或@組成"        },        enterpriseName: {            exp: /^[a-zA-Z\u4e00-\u9fa5()()_]+$/,            message: "企業名稱只能輸入中英文、底線或括弧"        },        license: {            exp: /^[0-9A-Za-z\-]+$/,            message: "營業執照號碼只允許數字英文中橫線"        },        address: {            exp: /^[0-9a-zA-Z\u4e00-\u9fa5#()()_-]+$/,            message: "公司地理位置只能包含中英文、數字、底線、中橫線、括弧和#號"        },        userName: {            exp: /^[a-zA-Z\u4e00-\u9fa5]+$/,            message: "姓名只能是中英文格式"        },        fixedTel: {            exp: /^[0-9\s,、,\-]*$/,            message: "辦公電話只能包含數字逗號頓號中橫線和空格"        },        jobName: {            exp: /^[0-9a-zA-Z\u4e00-\u9fa5()()_]+$/,            message: "職位名稱只能包含中英文數字底線或括弧"        },        recruitNum: {            exp: /^\+?[1-9][0-9]*$/,            message: "招聘人數只能是非0正整數~"        },        salary: {            exp: /^([1-9]\d*|0)(\.\d{1,2})?$/,            message: "薪酬可以是正整數或包含兩位小數"        },        height: {            exp: /^\d{2,3}$/,            message: "身高只能是2-3位的整數"        },        email: {            exp: /^(\w-*\.*)[email protected](\w-?)+(\.\w{2,})+$/,            message: "請檢查郵箱格式"        },        money: {            exp: /^(([1-9]\d*)|\d)(\.\d{1,2})?$/,            message: "請輸入正確的支付金額"        },        regCode: {            exp: /^\d{4}$/,            message: "請輸入正確的驗證碼"        }    }}
View Code

 

utils.js檔案;一些常用方法的備份

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.