常用JS

[javascript]
//——————–以下通用函數  
/**
 * format方法。
 * eg:
 *兩種調用方式
 *var template1="我是{0},今年{1}瞭";
 *var template2="我是{name},今年{age}瞭";
 *var result1=template1.format("loogn",22);
 *var result2=template1.format({name:"loogn",age:22});
 *兩個結果都是"我是loogn,今年22瞭"
 * @param {} args
 * @return {String}
 */ 
String.prototype.format = function(args) { 
    if (arguments.length>0) { 
        var result = this; 
        if (arguments.length == 1 && typeof (args) == "object") { 
            for (var key in args) { 
                var reg=new RegExp ("({"+key+"})","g"); 
                result = result.replace(reg, args[key]); 
            } 
        } 
        else { 
            for (var i = 0; i < arguments.length; i++) { 
                if(arguments[i]==undefined) 
                { 
                    return ""; 
                } 
                else 
                { 
                    var reg=new RegExp ("({["+i+"]})","g"); 
                    result = result.replace(reg, arguments[i]); 
                } 
            } 
        } 
        return result; 
    } 
    else { 
        return this; 
    } 
}; 
var errorHtml = '<b id="{id}_error" class="unsErrorClass" style="color:red;">{message}</b>'; 
var errorHtml_br = '<br /><b id="{id}_error" class="unsErrorClass" style="color:red;">{message}</b>'; 
 
function showMessage(p_id, p_message) { 
    farmatMessage(1, p_id, p_message); 

 
function cleanMessage(p_id) { 
    //$("#"+ p_id + "_error").html('');  
    $("#"+ p_id + "_error").remove(); 

 
function showBrMessage(p_id, p_message) { 
    farmatMessage(2, p_id, p_message); 

 
function farmatMessage(format, p_id, p_message) { 
    var error; 
    if(format == 1) { 
        error = errorHtml.format({id:p_id, message:p_message}); 
    } else { 
        error = errorHtml_br.format({id:p_id, message:p_message}); 
    } 
    if($("#"+ p_id + "_error").length > 0) { 
        $("#"+ p_id + "_error").html(p_message); 
    } else { 
        $("#"+ p_id).after(error); 
    } 

 
/*
 * 判空。 左右空白驗證時會忽略。
 */ 
function isNull(str){ 
    if(str != null && str != '' 
        && str.Trim()!='') { 
        return false; 
    } 
    return true; 

 
String.prototype.isNull = function() {  
    if(this != null && this != '' 
        && this.Trim()!='') { 
        return false; 
    } 
    return true; 
}; 
 
//去空格  
String.prototype.Trim = function() {  
    var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);  
    return (m == null) ? "" : m[1];  
}; 
String.prototype.Ltrim = function(){ return this.replace(/^\s+/g, "");}; 
String.prototype.Rtrim = function(){ return this.replace(/\s+$/g, "");}; 
 
//驗證手機號  
String.prototype.isMobile = function() {  
    var re = /^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$/; 
    return (re.test(this.Trim()));  
}; 
 
//驗證電話  
//:/^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/  
///^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/  //"兼容格式: 國傢代碼(2到3位)-區號(2到3位)-電話號碼(7到8位)-分機號(3位)"  
String.prototype.isTel = function(){ 
     
    return (/^((0\d{2,3})-)(\d{7,8})?$/.test(this.Trim())); 
}; 

//——————–以下通用函數
/**
 * format方法。
 * eg:
 *兩種調用方式
 *var template1="我是{0},今年{1}瞭";
 *var template2="我是{name},今年{age}瞭";
 *var result1=template1.format("loogn",22);
 *var result2=template1.format({name:"loogn",age:22});
 *兩個結果都是"我是loogn,今年22瞭"
 * @param {} args
 * @return {String}
 */
String.prototype.format = function(args) {
    if (arguments.length>0) {
        var result = this;
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                var reg=new RegExp ("({"+key+"})","g");
                result = result.replace(reg, args[key]);
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if(arguments[i]==undefined)
                {
                    return "";
                }
                else
                {
                    var reg=new RegExp ("({["+i+"]})","g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
        return result;
    }
    else {
        return this;
    }
};
var errorHtml = '<b id="{id}_error" class="unsErrorClass" style="color:red;">{message}</b>';
var errorHtml_br = '<br /><b id="{id}_error" class="unsErrorClass" style="color:red;">{message}</b>';

function showMessage(p_id, p_message) {
 farmatMessage(1, p_id, p_message);
}

function cleanMessage(p_id) {
 //$("#"+ p_id + "_error").html('');
 $("#"+ p_id + "_error").remove();
}

function showBrMessage(p_id, p_message) {
 farmatMessage(2, p_id, p_message);
}

function farmatMessage(format, p_id, p_message) {
 var error;
 if(format == 1) {
  error = errorHtml.format({id:p_id, message:p_message});
 } else {
  error = errorHtml_br.format({id:p_id, message:p_message});
 }
 if($("#"+ p_id + "_error").length > 0) {
  $("#"+ p_id + "_error").html(p_message);
 } else {
  $("#"+ p_id).after(error);
 }
}

/*
 * 判空。 左右空白驗證時會忽略。
 */
function isNull(str){
 if(str != null && str != ''
  && str.Trim()!='') {
  return false;
 }
 return true;
}

String.prototype.isNull = function() {
 if(this != null && this != ''
  && this.Trim()!='') {
  return false;
 }
 return true;
};

//去空格
String.prototype.Trim = function() {
 var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);
 return (m == null) ? "" : m[1];
};
String.prototype.Ltrim = function(){ return this.replace(/^\s+/g, "");};
String.prototype.Rtrim = function(){ return this.replace(/\s+$/g, "");};

//驗證手機號
String.prototype.isMobile = function() {
 var re = /^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$/;
 return (re.test(this.Trim()));
};

//驗證電話
//:/^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/
///^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/  //"兼容格式: 國傢代碼(2到3位)-區號(2到3位)-電話號碼(7到8位)-分機號(3位)"
String.prototype.isTel = function(){
   
    return (/^((0\d{2,3})-)(\d{7,8})?$/.test(this.Trim()));
};

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *