common.js

/// --------------------------------------------------
/// 数値チェック
/// --------------------------------------------------
function isNumeric(s) {
    // 空文字でも数値でもない場合、false
    if (s != '' && s.match(/[^0-9]/g)) {
        return false;
    }
    return true;
}

/// --------------------------------------------------
/// 数値チェック
/// --------------------------------------------------
function isTelNo(s) {
    // 空文字でも数値/-でもない場合、false
    if (s != '' && s.match(/[^0-9-]/g)) {
        return false;
    }
    return true;
}

/// --------------------------------------------------
/// 時間チェック
/// --------------------------------------------------
function isHour(s) {
    // 空文字、0~23は、true
    if (s == '' || s.match(/([0-1][0-9]|2[0-3]|^[0-9]$)/)) {
        return true;
    }
    return false;
}

/// --------------------------------------------------
/// 分チェック
/// --------------------------------------------------
function isMinute(s) {
    // 空文字、00~59は、true
    if (s == '' || s.match(/([0-5][0-9]|^[0-9]$)/)) {
        return true;
    }
    return false;
}

// --------------------------------------------------
// 数値大小チェックチェック
// --------------------------------------------------
function checkNumCompare(sLarge, sSmall) {
    return sLarge >= sSmall;
}

/// --------------------------------------------------
/// 3桁区切りのカンマ編集を行う。
/// --------------------------------------------------
function editComma(s) {
    returnValue = s;

    for (i4 = 0; i4 < returnValue.length / 3; i4++) {
        returnValue = returnValue.replace(/^([+-]?d+)(ddd)/, "$1,$2");
    }

    return returnValue;
}

/// --------------------------------------------------
/// 小数点あり3桁区切りのカンマ編集を行う。
/// --------------------------------------------------
function editCommaPoint(x) {

    delComma(x);

    var s = "" + x; // 文字列型に変換する
    var p = s.indexOf("."); // 小数点の位置を0オリジンで求める
    if (p < 0) { // 小数点が見つからなかった時
        p = s.length; // 仮想的な小数点の位置とする
    }
    var r = s.substring(p, s.length); // 小数点の桁と小数点より右側の文字列
    for (var i = 0; i < p; i++) { // (10 ^ i) の位について
        var c = s.substring(p - 1 - i, p - 1 - i + 1); 
        if (c < "0" || c > "9") { // 数字以外のもの(符合など)が見つかった
            r = s.substring(0, p - i) + r; // 残りを全部付加する
            break;
        }
        if (i > 0 && i % 3 == 0) { // 3 桁ごと、ただし初回は除く
            r = "," + r; // カンマを付加する
        }
        r = c + r; // 数字を一桁追加する。
    }
    return r; 
}

// -----------------------------------
// カンマ削除関数
// -----------------------------------
function delComma(str) {
    return str.split(",").join("");
}

// -----------------------------------
// 小数点以下3桁四捨五入
// -----------------------------------
function addNumber(i) {
    var n = i * 100;
    n = Math.round(n);
    return n / 100;
}

// -----------------------------------
// 画面内の全ボタンを使用不可にする。
// -----------------------------------
function disableAllButton() {

    // ポップアップを先に表示する
    // ウィンドウオブジェクトを生成
    popObj = window.createPopup();
    // アドレスを短くするために、変数magicに「robot.document.body」を代入
    popBodyObj = popObj.document.body;
    // 枠(ボーダー)の太さを2px、色をブルーに変更 
    popBodyObj.style.border = "solid 2px blue";
    // gifのURL取得(仮想ディレクトリまでのURLを取得して絶対パスを設定)
    var gifUrl = location.href;
    var baseUrl = location.href.slice(0, location.href.indexOf(location.pathname));
    // pathnameの先頭が「/dialog/」である場合または「/」が一つしかない(本番環境と判定)
    if (location.pathname.search(//(dialog|form)//i) == 0
        || location.pathname.indexOf('/', 1) < 0) {
        gifUrl = baseUrl;
    }
    else {
        gifUrl = baseUrl + location.pathname.slice(0, location.pathname.indexOf('/', 1));
    }
    gifUrl += '/img/proessing.gif';
    // ポップアップ内のHTMLにイメージとメッセージを書き込む
    popBodyObj.innerHTML = "<center><br /><div style="position:relative;80px;height:80px;text-align:center;"><span class="guard"style="top:0px;left:0px;"></span></div><br />ただ今処理中です。<br />お待ちください・・・</center><style type="text/css">span.guard{position:absolute;display:block;80px;height:80px;background-image:url(" + gifUrl + ");}</style>";
    // ポップアップを画面上に表示
    windowWidth = 200;
    windowHeight = 180;
    x = (document.body.clientWidth / 2) - (windowWidth / 2);
    y = (document.body.clientHeight / 2) - (windowHeight);
    // 値は左から順に(x座標, y座標, width, height, document.body)となっています。
    popObj.show(x, y, windowWidth, windowHeight, document.body);

    //ボタンの不活性化
    setTimeout(function() {
        //        ///////////////
        //        //計測用:開始
        //        var st = new Date();
        //        ///////////////

        //button
        var elements = $(".button");

        elements.each(function(i, elem) {
            $(this).attr("disabled", "disabled");
        });

        //text,change他
        //検索系の画面しかないため、コメントアウト

        //        elements = $(".textboxImeOn,.textboxImeOff,.textboxImeDisabled");
        //        elements.each(function(i, elem) {
        //            $(this).unbind("onchange");
        //        });

        //解放処理
        elements = null;

        //        ///////////////
        //        //計測用:終了
        //        var ed = new Date();
        //        //計測用:表示
        //        var dif = ed.getTime() - st.getTime();
        //        alert(dif);
        //        ///////////////
    }, 0);
}

// --------------------------------------------------
// 文字列バイト数取得
// --------------------------------------------------
function getByte(s) {
    var count = 0;
    for (i = 0; i < s.length; i++) {
        n = escape(s.charAt(i));
        if (n.length < 4) count++;
        else count += 2;
    }
    return count;
}

// --------------------------------------------------
// 文字列バイト数チェック
// --------------------------------------------------
function checkByte(s, maxByte) {
    var count = getByte(s);
    
    if (count > maxByte) {
        return false;
    }
    return true;
}

// --------------------------------------------------
// 日付フォーマット変換
// --------------------------------------------------
function changeDateFormat(s) {
    var dateStr = s;
    var mm;
    var dd;
    // 文字列に「/」がある場合
    if (s.indexOf('/') >= 0) {
        a = s.split('/');
        // 文字列に「/」が2つある場合
        if (a.length == 3) {
            if (a.length >= 2 && a[1].length == 1) {
                mm = '0' + a[1];
            } else if (a.length >= 2 && a[1].length == 2) {
                mm = a[1];
            } else {
                mm = '';
            }
            
            if (a.length == 3 && a[2].length == 1) {
                dd = '0' + a[2];
            } else if (a.length == 3 && a[2].length == 2) {
                dd = a[2];
            } else {
                dd = '';
            }
            dateStr = a[0] + '/' + mm + '/' + dd;
        }
    } else if (s.length == 8) {
        dateStr = s.substring(0, 4) + '/' + s.substring(4, 6) + '/' + s.substring(6, 8)
    }
    return dateStr;
}

// --------------------------------------------------
// 日付妥当性チェック
// --------------------------------------------------
function isDate(s) {
    var datestr = changeDateFormat(s);
    if (s == "") return true;
    // 正規表現による書式チェック 
    if (datestr.match(/^d{4}/d{2}/d{2}$/)) {
        var vYear = datestr.substr(0, 4) - 0;
        var vMonth = datestr.substr(5, 2) - 1; // Javascriptは、0-11で表現
        var vDay = datestr.substr(8, 2) - 0;
        
        // 月,日の妥当性チェック 
        if (vMonth >= 0 && vMonth <= 11 && vDay >= 1 && vDay <= 31) {
            var vDt = new Date(vYear, vMonth, vDay);
            if (isNaN(vDt)) {
                // OK
                return true;
            } else if (vDt.getFullYear() == vYear && vDt.getMonth() == vMonth && vDt.getDate() == vDay) {
                // OK
                return true;
            }
        }
    }
    return false;
}


// --------------------------------------------------
// BackSpace キーを無効にする
// --------------------------------------------------
window.document.onkeydown = onKeyDown;
function onKeyDown(e) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        //ALT+← ダメ 
        if (event.keyCode == 0x25 && event.altKey == true) {
            //alert("ALT+←はダメ!"); 
            return false;
        }
        //テキストボックス、パスワードボックスは許す 
        for (i = 0; i < document.all.tags("INPUT").length; i++) {
            if (document.all.tags("INPUT")(i).name == window.event.srcElement.name && (document.all.tags("INPUT")(i).type == "text" || document.all.tags("INPUT")(i).type == "password") && document.all.tags("INPUT")(i).readOnly == false) {
                return true;
            }
        }
        //テキストエリアは許す 
        for (i = 0; i < document.all.tags("TEXTAREA").length; i++) {
            if (document.all.tags("TEXTAREA")(i).name == window.event.srcElement.name && document.all.tags("TEXTAREA")(i).readOnly == false) {
                return true;
            }
        }
        //BackSpaceダメ 
        if (event.keyCode == 8) {
            //alert("BackSpaseはダメ!"); 
            return false;
        }
    } else
        if (navigator.appName == "Netscape") {
        if (e.which == 8) {
            return false;
        }
    }
}

// --------------------------------------------------
// F5 CTRL+Rを無効にする
// --------------------------------------------------
document.onkeydown = forbidkeys;
function forbidkeys() {
    switch (event.keyCode) {
        case 116:
        case 82:
            event.keyCode = 0;
            return false;
            break;
    }
}

// --------------------------------------------------
// 文字数をカウント
// 半角は2文字までは1カウント
// strSrc:カウント対象文字列
// --------------------------------------------------
function strLength(strSrc) {
    len = 0;
    strSrc = escape(strSrc);
    for (i = 0; i < strSrc.length; i++, len++) {
        if (strSrc.charAt(i) == "%") {
            if (strSrc.charAt(++i) == "u") {
                i += 3;
                len++;
            }
            i++;
        }
    }
    return len;
}


// --------------------------------------------------
// 残りの入力可能文字を表示
// name:id名、str:入力文字列、stopCount:最大入力可能文字数
// --------------------------------------------------
function CountDownLength(name, str, stopCount) {
    var strCount = Math.ceil(strLength(str).toString() / 2);
    var message;
    if (stopCount - strCount >= 0) {
        message = "(あと" + (stopCount - strCount) + "文字)";
        document.getElementById(name).style.color = 'black';
    }
    else {
        message = "(" + (strCount - stopCount) + "文字超え)";
        document.getElementById(name).style.color = 'red';
    }
    
    document.getElementById(name).innerHTML = message;
}


// --------------------------------------------------
// ラジオボタンの選択されているvalue値を取得する
// --------------------------------------------------
function radioValue(element) {
    var len;
    len = element.length;
    for (i = 0; i < len; i++) {
        if (element[i].checked) {
            return element[i].value;
        }
    }

    return "";
}


// -----------------------------------
// Windowのスクロール位置を画面に設定する。
// 第1引数:縦スクロール位置を格納しているhiddenID
// 第2引数:横スクロール位置を格納しているhiddenID
// -----------------------------------
function setWindowScroll(hiddenVerticalId, hiddenHorizonId) {
    var verScrVal = 0;
    var horScrVal = 0;

    var verObj = document.getElementById(hiddenVerticalId);
    if (verObj != null && verObj != undefined) {
        verScrVal = verObj.value
    }
    var horObj = document.getElementById(hiddenHorizonId);
    if (horObj != null && horObj != undefined) {
        horScrVal = horObj.value
    }

    window.scroll(horScrVal, verScrVal);

}


// -----------------------------------
// GridViewのスクロール位置を画面に設定する。
// 第1引数:対象GridViewのdivID
// 第2引数:縦スクロール位置を格納しているhiddenID
// 第3引数:横スクロール位置を格納しているhiddenID
// -----------------------------------
function setGridViewScroll(divId, hiddenVerticalId, hiddenHorizonId) {
    var odj = document.getElementById(divId);
    if (odj != null && odj != undefined) {
        var verObj = document.getElementById(hiddenVerticalId);
        if (verObj != null && verObj != undefined) {
            document.getElementById(divId).scrollTop =
document.getElementById(hiddenVerticalId).value;
        }
        var horObj = document.getElementById(hiddenHorizonId);
        if (horObj != null && horObj != undefined) {
            document.getElementById(divId).scrollLeft =
document.getElementById(hiddenHorizonId).value;
        }

    }
}


// -----------------------------------
// Windowのスクロール位置をhiddenに設定する。
// 第1引数:縦スクロール位置を格納するhiddenID
// 第2引数:横スクロール位置を格納するhiddenID
// -----------------------------------
function getWindowScroll(hiddenVerticalId, hiddenHorizonId) {
    var verObj = document.getElementById(hiddenVerticalId);
    if (verObj != null && verObj != undefined) {
        document.getElementById(hiddenVerticalId).value =
document.body.scrollTop;
    }
    var horObj = document.getElementById(hiddenHorizonId);
    if (horObj != null && horObj != undefined) {
        document.getElementById(hiddenHorizonId).value =
document.body.scrollLeft;
    }
}

// -----------------------------------
// GridViewのスクロール位置をhiddenに設定する。
// 第1引数:対象GridViewのdivID
// 第2引数:縦スクロール位置を格納するhiddenID
// 第3引数:横スクロール位置を格納するhiddenID
// -----------------------------------
function getGridViewScroll(divId, hiddenVerticalId, hiddenHorizonId) {
    if (!!document.getElementById(divId)) {
        var verObj = document.getElementById(hiddenVerticalId);
        if (verObj != null && verObj != undefined) {
            document.getElementById(hiddenVerticalId).value = document.getElementById(divId).scrollTop;
        }
        var horObj = document.getElementById(hiddenHorizonId);
        if (horObj != null && horObj != undefined) {
            document.getElementById(hiddenHorizonId).value = document.getElementById(divId).scrollLeft;
        }
    }
}

// -----------------------------------
// 日付より曜日を取得する。
// 第1引数:日付(文字列)
// -----------------------------------
function getDayFromDate(strDate) {

       var myDate = new Date(strDate);
        DW = new Array('日', '月', '火', '水', '木', '金', '土') ;
        return DW[myDate.getDay()];

}

// -----------------------------------
// TextAreaを縦方向に自動的に広げる
// 第1引数:event
// onkeyup="resize_textarea(event)"のように設定
// -----------------------------------
function resizeTextarea(ev) {
    if (ev) {
        //if (ev.keyCode != 13) return;
        var textarea = ev.target || ev.srcElement;
        var value = textarea.value;
        var lines = 1;
        for (var i = 0, l = value.length; i < l; i++) {
            if (value.charAt(i) == '
') lines++;
        }
        textarea.setAttribute("rows", lines + 1);
        // window.status = lines;
    }
}

// -----------------------------------
// マッチしたtextareaタグのidのresizeTextAreaを実行する
// 第1引数:id
// -----------------------------------
function raiseResizetextArea(id) {
    var target = $(document.getElementById(id)).find("textarea");

    target.each(function(ind, elem) {
        var ev = new Object();
        ev.srcElement = elem;

        //eventの代わりにセットする
        resizeTextarea(ev);
    });
}
View Code
原文地址:https://www.cnblogs.com/haiy/p/4146314.html