項目常用Javascript分享,包含常用驗證和Cookie操作

function IsEmail(str) {
    var r = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (r.test(str)) {
        return true;
    } else {
        return false;
    }
}

function IsTel(str) {
    var s = /^(\d{2,4})(-(\d{7,8}))$/;
    if (s.test(str)) {
        return true;
    } else {
        return false;
    }
}

function IsMobile(str) {
    var regu = /^[1][3,5,8][0-9]{9}$/;
    var re = new RegExp(regu);
    if (re.test(str)) {
        return true;
    }
    else {
        return false;
    }
}

function IsDecimal(str) {
    if (str == "") { return false; }
    if ((/^\-?(([1-9]\d*)|\d)(\.\d{1,4})?$/).test(str)) {
        return true;
    }
    else {
        return false;
    }
}

function IsInt(str) {
    if (str == "") { return false; }
    if (str == "0") { return false; }
    var type = "^[0-9]*[1-9][0-9]*$";
    var re = new RegExp(type);
    if (str.match(re) == null) {
        return false;
    }
    else {
        return true;
    }
}

function isChinaOrNumbOrLett(s) {
    //判斷是否是漢字、字母、數字組成    
    var regu = "^[0-9a-zA-Z\u4e00-\u9fa5]+$";
    var re = new RegExp(regu);
    if (re.test(s)) {
        return true;
    }
    else {
        return false;
    }
}

function LTrim(str) {
    return str.replace(/^[ \t\n\r]+/g, "");
}

function RTrim(str) {
    return str.replace(/[ \t\n\r]+$/g, "");
}

function Trim(str) {
    return RTrim(LTrim(str));
}

/**************
*把輸入的字母轉成大寫。
*
**************/
function input_to_uppercase(obj) {
    obj.value = obj.value.toUpperCase();
}


/**************
*把輸入的字母轉成小寫。
* 
**************/
function input_to_lowercase(obj) {
    obj.value = obj.value.toLowerCase();
}

/**************
*實時檢測輸入框的字數

文字最大長度: 20. 還剩: 20.
**************/
function check_input_Length(which) {
    var maxChars = 20;
    if (which.value.length > maxChars)
        which.value = which.value.substring(0, maxChars);
    var curr = maxChars - which.value.length;
    document.getElementById("chLeft").innerHTML = curr.toString();
}


//將日期轉換成"yyyymmdd"格式 第二個參數為返回類型傳入'ym':yyyy-mm,'md':mm-dd,默認為'yyyy-mm-dd'
function ConvertDateFormatString(strDate, sye) {
    if (strDate == null || strDate == "" || strDate == "0" || strDate.length < 8) {
        return ("");
    }
    else {
        try {
            var ExDate = new Date(strDate.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1/$2/$3"));
            var yyyy = ExDate.getFullYear();
            var mm = ExDate.getMonth() + 1; mm = mm < 10 ? "0" + mm : mm;
            var dd = ExDate.getDate(); dd = dd tbody tr:even").addClass("NormalColor");
    //first
    $(gridviewId + ">tbody tr:first").removeClass("NormalColor").addClass("HeadColor");
    //odd
    $(gridviewId + ">tbody tr:odd").addClass("AlterColor");
    //move and click
    $(gridviewId + ">tbody tr").slice(1).hover(function () {
        $(this).addClass("HoverColor");
    }, function () {
        $(this).removeClass("HoverColor");
    });
    //all check
    $("#chkAll").click(function () {
        $(gridviewId + '>tbody >tr >td >input:checkbox').attr('checked', this.checked);
    });
    //check status
    $(gridviewId + ' >tbody >tr >td >input:checkbox').click(function () {
        var expr1 = gridviewId + ' >tbody >tr >td >input:checkbox:checked';
        var expr2 = gridviewId + ' >tbody >tr >td >input:checkbox';
        var selectAll = $(expr1).length == $(expr2).length;
        $('#chkAll').attr('checked', selectAll);
    });
}

//show easyUI load
function Load() {
    $("

").css({ display: "block", width: "100%", height: $(window).height() }).appendTo("body"); $("

").html("正在運行,請稍候。。。").appendTo("body").css({ display: "block", left: ($(document.body).outerWidth(true) - 190) / 2, top: ($(window).height() - 45) / 2 }); } //display easyUI Load function dispalyLoad() { $(".datagrid-mask").remove(); $(".datagrid-mask-msg").remove(); } //show loading function showLoad(tipInfo) { var eTip = document.createElement('p'); eTip.setAttribute('id', 'tipDiv'); eTip.style.position = 'absolute'; eTip.style.display = 'none'; eTip.style.border = 'solid 0px #D1D1D1'; eTip.style.backgroundColor = '#4B981D'; eTip.style.padding = '5px 15px'; eTip.style.top = '10px'; eTip.style.right = '10px'; eTip.style.width = '120px'; eTip.innerHTML = ' ' + tipInfo + ''; try { document.body.appendChild(eTip); } catch (e) { } $("#tipDiv").css("float", "right"); $("#tipDiv").css("z-index", "99"); $('#tipDiv').fadeIn(); } //display loading function closeLoad() { $('#tipDiv').fadeOut(); } //open JqueryDialog function showMyModalDialog(url, width, height, Title) { var now = new Date(); var urlr if (url.indexOf('=') == -1) { urlr = url + "?cach=" + escape(now); } else { urlr = url + "&cach=" + escape(now); } // add zjc if (Title != null) { JqueryDialog.Open(Title, urlr, width, height); } else { JqueryDialog.Open('', urlr, width, height); } } //close JqueryDialog function DialogCloseReload(url, info) { alert(info); window.parent.location.href = url; window.parent.JqueryDialog.Close(); } //confirm dialog //if(ConfirmDialog('確認刪除數據?')){alert('開始刪除');} function ConfirmDialog(info) { if (confirm(info)) { return true; } else { return false; } } /** * ajax post提交 * @param url * @param param * @param datat 為html,json,text * @param callback回調函數 * @return */ function jsonAjax(url, param, datat, callback) { $.ajax({ type: "post", url: url, data: param, dataType: datat, success: callback, error: function () { jQuery.fn.mBox({ message: '恢復失敗' }); } }); } /* check alert checkAlert(IsEmail($("#txtEmail").val()), '請輸入正確的郵箱地址'); */ function checkAlert(fn, info) { if (fn == false) { alert(info); return; } } /* check alert disabled checkAlert(IsEmail($("#txtEmail").val()), '請輸入正確的郵箱地址','btnSubmit'); */ function checkAlert(fn, info, objId) { if (fn == false) { alert(info); $("#" + objId).attr("disabled", true); return false; } else { $("#" + objId).attr("disabled", false); return true; } } /* check to message var str = ""; str += checkString(IsEmail($("#txtEmail").val()), '請輸入正確的郵箱地址'); str += checkString(IsMobile($("#txtMobile").val()), '請輸入正確的手機號碼'); */ function checkString(fn, info) { if (fn == false) { return info +"\n"; } else { return ""; } }

發佈留言