Поиск по этому блогу

воскресенье, 9 ноября 2014 г.

Набор полезных JS скриптов

Сюда входят:
1 использование jquery элемента blockUI
2 проверка полей телефона и поля суммы
3 работа с cookie



/******************************BlockUI*************************************************/

var URL = '';

$(document).ready(function () {

    $(".nextButton").click(function () {
        console.log("button pressed");
        openBlockUI();
    });
    $(".payButton").click(function () {
        console.log("button pressed");
        openBlockUI();
    });
    $('#yes').bind('click', $.unblockUI);
    $('#yes').click(function () {
        if (URL)
            document.location.href = URL;
    });
});
function errorDialogStart(msgs, url) {
    URL = url;
    if (msgs) {
        $('.blockUI_H3_Update').html(msgs);
    }
    $.blockUI({
        message: $('#question'),
        css: {
            width: '480px',
            background: 'linear-gradient(to bottom, #EEF8FE 0%, #A2DAFE 100%)'
        }
    });

}

function openBlockUI() {
    $.blockUI(
    {
        message: '<h1 class="blockUI_H1"">Loading...</h1><h3 class="blockUI_H3">Please wait...</h3>',
        css: {
            border: 'none',
            backgroundColor: '#f00',
            background: 'linear-gradient(to bottom, #EEF8FE 0%, #A2DAFE 100%)',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            color: '#434A86',
            width: '460',
            minHeight: '50'
        }
    });
}

function hideBlockUI() {
    getStatusBlockUI();
    if ((isPostBack() == 'true')) {
        $.unblockUI();
    }
}


function isPostBack() {
    var ispostback = getStatusBlockUI();
    return ispostback;
}

function getNameCookie() {
    var k = document.location.pathname.match(/[^.\/]+/g);
    return '_ispostback' + k[k.length - 2];
}

function setStatusBlockUI(status) {
    $('#statusblockUI').val(status);
}

function getStatusBlockUI() {
    var status = $('#statusblockUI').val();
    return status;
}
/*************************************************************************************/

/**************************************Cookie******************************************/

// возвращает cookie с именем name, если есть, если нет, то undefined
function getCookie(name) {
    var matches = document.cookie.match(new RegExp(
      "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
  ));
    return matches ? decodeURIComponent(matches[1]) : undefined;
}

function setCookie(name, value, options) {
    options = options || {};

    var expires = options.expires;

    if (typeof expires == "number" && expires) {
        var d = new Date();
        d.setTime(d.getTime() + expires * 1000);
        expires = options.expires = d;
    }
    if (expires && expires.toUTCString) {
        options.expires = expires.toUTCString();
    }

    value = encodeURIComponent(value);

    var updatedCookie = name + "=" + value;

    for (var propName in options) {
        if (options.hasOwnProperty(propName)) {
            updatedCookie += "; " + propName;
            var propValue = options[propName];
            if (propValue !== true) {
                updatedCookie += "=" + propValue;
            }
        }
    }
    document.cookie = updatedCookie;
}

function deleteCookie(name) {
    setCookie(name, "", { expires: -1 });
}

/*************************************************************************************/


/**********************************ValidateFields*************************************/
function validateAmount() {
    $('#ContentPlaceHolder1_PhoneBox').keypress(function(key) {
        var c = String.fromCharCode(key.which);
        Temp = $(this).val();
        if ($(this).val().length != 0) {
            if (key.which == 46) {
            } else if (key.which >= 48 && key.which <= 57) {
                if ($(this).val().length >= 16) {
                    key.preventDefault();
                }
            } else if (key.which == 8 || key.keyCode == 37 || key.keyCode == 39
                || key.which == 127 || key.ctrlkey || key.which == 118 || key.which == 99 || key.which == 0) {
                if (key.which == 8) {

                }
            } else {
                key.preventDefault();
            }
        } else {
            if (key.which >= 48 && key.which <= 57) {
            } else if (key.which == 8 || key.which == 37 || key.which == 39
                || key.which == 127 || key.which == 118 || key.which == 99 || key.which == 0) {
            } else {
                key.preventDefault();
            }
        }
    });
    $('#ContentPlaceHolder1_PhoneBox').on('paste', function() {

        $(this).val($(this).val().replace(/[^0-9]/g, ''));
    });
    $('#ContentPlaceHolder1_PhoneBox').on('keydown keyup input', function(key) {
        console.log(key.which);
        if ($(this).val().length >= 16) {
            if (key.which == 8 || (key.which >= 37 && key.which <= 40)
                || key.which == 127 || key.ctrlkey || key.which == 118
                || key.which == 99 || key.which == 0 || key.which == 46 || key.which == 65) {

            } else {
                key.preventDefault();
            }
        }
        if ((key.which < 35 || key.which > 40) && key.which != 17
            && key.which != 8 && key.which != 45 && key.which != 46
            && (key.which < 48 || key.which > 57) && key.which != 65) {
            $(this).val($(this).val().replace(/[^0-9]/g, ''));
        }

        return true;
    });

    var Temp;
    var int_num_allow = 3;
    var float_num_allow = 2;
    $("#ContentPlaceHolder1_AmountBox").keypress(function(key) {
        var c = String.fromCharCode(key.which);
        Temp = $(this).val();
        if ($(this).val().length != 0) {
            var iof = $(this).val().indexOf(".");

            if (iof != -1) {
                if (key.which == 46) {
                    key.preventDefault();
                } else if (key.which >= 48 && key.which <= 57) {
                    if ($(this).val().length == int_num_allow + float_num_allow + 1) {
                        key.preventDefault();
                    }
                } else if (key.which == 8 || key.keyCode == 37 || key.keyCode == 39
                    || key.which == 127 || key.ctrlkey || key.which == 118 || key.which == 99 || key.which == 0) {
                    if (key.which == 8) {
                        if ($(this).val() == "0." || $(this).val() == "00." || $(this).val() == "000.") {
                            $(this).val('');
                        }
                    }
                } else {
                    key.preventDefault();
                }
            } else {
                if (key.which == 46) {
                } else if (key.which >= 48 && key.which <= 57) {
                    if ($(this).val().length >= int_num_allow) {
                        key.preventDefault();
                    }
                } else if (key.which == 8 || key.keyCode == 37 || key.keyCode == 39
                    || key.which == 127 || key.ctrlkey || key.which == 118 || key.which == 99 || key.which == 0) {
                    if (key.which == 8) {
                        if ($(this).val() == "0." || $(this).val() == "00." || $(this).val() == "000.") {
                            $(this).val('');
                        }
                    }
                } else {
                    key.preventDefault();
                }
            }
        } else {
            if (c == "0") {
                $("#ContentPlaceHolder1_AmountBox").val("0.");
                key.preventDefault();
            }
            if (c == ".") {
                $("#ContentPlaceHolder1_AmountBox").val("0.");
                key.preventDefault();
            }
            if (key.which >= 48 && key.which <= 57) {
            } else if (key.which == 8 || key.which == 37 || key.which == 39
                || key.which == 127 || key.which == 118 || key.which == 99 || key.which == 0) {
            } else {
                key.preventDefault();
            }

        }
    });
    $('#ContentPlaceHolder1_AmountBox').on('paste', function() {

        $(this).val($(this).val().replace(/[^0-9.]/g, ''));

    });
    $('#ContentPlaceHolder1_AmountBox').on('keydown keyup input', function(key) {

        var iof = $(this).val().indexOf(".");


        if ($(this).val()[0] == "0" && $(this).val()[1] != ".") {
            $(this).val($(this).val().substring(1, $(this).val().length));
        }

        if (iof != -1) {

            if ($(this).val().split('.').length - 1 > 1) {
                var Repl = "";
                for (i = 0; i < $(this).val().length; i++) {
                    var T = $(this).val()[i];
                    if (T == ".") {
                        if (i != iof) {
                            T = "";
                        }
                    }
                    Repl += T;
                }
                $(this).val(Repl.replace(' ', ''));
            }

            if ($(this).val().substring(0, iof).length > int_num_allow && $(this).val().length > 0) {
                $(this).val(Temp);
                $(this).val($(this).val().replace(/[^0-9.]/g, ''));
            }

            if ($(this).val().substring(iof, $(this).val().length - 1).length > float_num_allow && $(this).val().length > 0) {
                $(this).val(Temp);
                $(this).val($(this).val().replace(/[^0-9.]/g, ''));
            }

            if (($(this).val().indexOf("00.") != -1 || $(this).val().indexOf("000.") != -1 ||
                $(this).val().indexOf("0.00") != -1) && $(this).val() == 0) {
                $(this).val('');
                $(this).attr('placeholder', 'invalid amout');
            }

            if (iof == 0) {
                $(this).val("0" + $(this).val());
            }

        } else {
            if ($(this).val().length >= int_num_allow + 1) {
                if (key.which == 8 || key.keycode == 37 || key.keycode == 39
                    || key.which == 127 || key.ctrlkey || key.which == 118 || key.which == 99 || key.which == 0 || key.which == 46) {
                    if (key.which == 8) {
                        if ($(this).val() == "0." || $(this).val() == "00." || $(this).val() == "000.") {
                            $(this).val('');
                        }
                    }

                } else {
                    key.preventDefault();
                }
            }
        }

        if (iof == -1 && $(this).val().length > int_num_allow) {
            $(this).val($(this).val().substring(0, 3));
            $(this).val($(this).val().replace(/[^0-9.]/g, ''));
        }

        if ((key.which < 35 || key.which > 40) && key.which != 17
            && key.which != 8 && key.which != 45 && key.which != 46
            && (key.which < 48 || key.which > 57)) {
            $(this).val($(this).val().replace(/[^0-9.]/g, ''));
        }

        if ($(this).val().length - iof >= 3 && iof != -1 && $(this).val() == 0) {
            $(this).val('');
            $(this).attr('placeholder', 'invalid amout');
        }

        return true;
    });


    $(".coloredTextBoxStyle").on('keyup', function(event) {
        event = event || window.event;

        isNotEmpty(event.target.name, 'coloredTextBoxStyle');
    });
};

function isNotEmpty(elemId, className) {
    var elemName = elemId;
    var elemAll = document.getElementsByClassName(className);
    var elemPanelAll = document.getElementsByClassName('errorDiv');
    for (var i = 0; i < elemAll.length; i++) {
        if (elemName == elemAll[i].name) {
            var str = elemAll[i].value;
            var re = /.+/;
            if (!str.match(re)) {
                elemAll[i].style.border = '2px inset red';
                if (elemPanelAll.length >= i + 1) {
                    elemPanelAll[i].style.visibility = 'visible';
                }
            } else {
                elemAll[i].style.border = '2px inset #7DB5DE';
                if (elemPanelAll.length >= i + 1) {
                    elemPanelAll[i].style.visibility = 'hidden';
                }
            }
        }
    }
};

Комментариев нет:

Отправить комментарий