Самая умная форма (маска) валидации телефона (ajax-forms and bitrix-cache friendly)

Особенность этого валидатора - не важно что вводит пользователь +7 или 8 - всегда будет то что надо.

//jquery mask - required
$APPLICATION->AddHeadScript('//cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js');

if (window.frameCacheVars !== undefined) {
$(document).on('click','.phone-input input, input[type="tel"], .inputphone, #PHONE_FID11',function(){
    BX.addCustomEvent("onFrameDataReceived", $('.phone-input input, input[type="tel"], .inputphone, #PHONE_FID11').each(function () {
        phonemask2($(this));
    }));
});
} else {
$(document).on('click','.phone-input input, input[type="tel"], .inputphone, #PHONE_FID11',function(){
    $('.phone-input input, input[type="tel"], .inputphone, #PHONE_FID11').each(function () {
        phonemask2($(this));
    });
});
}


function phonemask2(thisobj){
    thisobj
        .focusin(function () {
            $(this).attr('placeholder', '+7(___)___-__-__');
        })
        .focusout(function () {
            $(this).attr('placeholder', 'Ваш телефон');
        })
        .mask(
            '+0(000)000-00-00',
            {
                clearIfNotMatch: true,
                onKeyPress: function (cep, event, currentField) {
                    var phn = cep.replace(/D+/g, "");
                    //var $submit = currentField.parents('form').find('[type="submit"]');
                    if (cep.length > 1 && cep.substr(0, 2) != '+7') {
                        if (cep.substr(0, 2) == '+8') {
                            currentField.val('+7' + cep.substr(2));
                        }
                        else {
                            currentField.val('+7' + cep.substr(1));
                        }
                    }
                    if (phn.length != 11) {
                        currentField.removeClass('good-input');
                    }
                    if (phn.length == 11) {
                        currentField.addClass('good-input');
                    }
                }
            }
        );
}
Афоризм дня:
Какой человек, будучи рабом удовольствий, не извратит своего тела и души? (668)

Leave a reply

Яндекс.Метрика