Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142353

Contact form Phone Number format Javascript

$
0
0

This piece of code lets me write a phone number to my contact form as (XXX) XXX-XXXX format. (working example is at https://www.fxmerkezi.com/ucretsiz-danismanlik/)

But I need it to be done like 0XXXXXXXXXX first character must be 0 and no letters or any other characters shouldt be allowed.

This is the code in my head tags;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://unpkg.com/jquery-input-mask-phone-number@1.0.0/dist/jquery-input-mask-phone-number.js"></script>

    <script>

        $(document).ready(function () {
            $('#yourphone').usPhoneFormat({
                format: '(xxx) xxx-xxxx',
            });

            $('#yourphone2').usPhoneFormat();
        });

    </script>

And this is the file jquery-input-mask-phone-number.js:

(function ($) {
$.fn.usPhoneFormat = function (options) {
    var params = $.extend({
        format: 'xxx-xxx-xxxx',
        international: false,

    }, options);

    if (params.format === 'xxx-xxx-xxxx') {
        $(this).bind('paste', function (e) {
            e.preventDefault();
            var inputValue = e.originalEvent.clipboardData.getData('Text');
            if (!$.isNumeric(inputValue)) {
                return false;
            } else {
                inputValue = String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3"));
                $(this).val(inputValue);
                $(this).val('');
                inputValue = inputValue.substring(0, 12);
                $(this).val(inputValue);
            }
        });
        $(this).on('keypress', function (e) {
            if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                return false;
            }
            var curchr = this.value.length;
            var curval = $(this).val();
            if (curchr == 3) {
                $(this).val(curval + "-");
            } else if (curchr == 7) {
                $(this).val(curval + "-");
            }
            $(this).attr('maxlength', '12');
        });

    } else if (params.format === '(xxx) xxx-xxxx') {
        $(this).on('keypress', function (e) {
            if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                return false;
            }
            var curchr = this.value.length;
            var curval = $(this).val();
            if (curchr == 3) {
                $(this).val('(' + curval + ')' + "");
            } else if (curchr == 9) {
                $(this).val(curval + "-");
            }
            $(this).attr('maxlength', '14');
        });
        $(this).bind('paste', function (e) {
            e.preventDefault();
            var inputValue = e.originalEvent.clipboardData.getData('Text');
            if (!$.isNumeric(inputValue)) {
                return false;
            } else {
                inputValue = String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3"));
                $(this).val(inputValue);
                $(this).val('');
                inputValue = inputValue.substring(0, 14);
                $(this).val(inputValue);
            }
        });

    }
}

}(jQuery));


Viewing all articles
Browse latest Browse all 142353

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>