手机固定号码美化

之前工作需求写的一个基于jquery的小工具,可以把手机号码美化成133-1024-1024,及固定电话号码美化成0755-6666666

(function ($) {
    $.fn.phoneView = function () {
        return this.each(function () {

            $(this).off('keyup.pv change.pv input.pv').
                on('keyup.pv change.pv input.pv', function (e) {
                var target = $(this),
                    inputVal = target.val(),
                    numVal = inputVal.replace(/-/g, ''),
                    showVal = '';

                if (/^[\d]*$/.test(numVal)) {
                    if(/^0[3-9]\d+$/.test(numVal)){
                        if(numVal.length > 4 && numVal.length < 13){
                            showVal = numVal.substring(0, 4) + "-" 
                                      + numVal.substring(4, numVal.length);
                        }else{
                            showVal = numVal;
                        }
                    }else if (/^0[1-2]\d+$/.test(numVal)) {
                        if(numVal.length > 3 && numVal.length < 12){
                            showVal = numVal.substring(0, 3) + "-"
                                      + numVal.substring(3, numVal.length);
                        }else{
                            showVal = numVal;
                        }
                    }else if (/^1[3-8]\d+$/.test(numVal)) {
                        if(numVal.length > 3 && numVal.length < 8){
                            showVal = numVal.substring(0, 3) + "-" 
                                      + numVal.substring(3, numVal.length);
                        }else if(numVal.length > 7 && numVal.length < 12){
                            showVal = numVal.substring(0, 3) + "-" + numVal.substring(3, 7)
                                      + '-' + numVal.substring(7, numVal.length);
                        }else{
                            showVal = numVal;
                        }
                    }else{
                        showVal = numVal;
                    }

                    target.val(showVal).attr('data-num', numVal);
                }

            });
        });
    };
    $.fn.phoneView.version = 1.0;
})(jQuery);

你可能感兴趣的:(手机固定号码美化)