Phone mask with jQuery and Masked Input Plugin

前端 未结 15 1396
夕颜
夕颜 2020-11-28 06:54

I have a problem masking a phone input with jQuery and Masked Input Plugin.

There are 2 possible formats:

  1. (XX)XXXX-XXXX
  2. (
相关标签:
15条回答
  • 2020-11-28 07:45

    I was developed simple and easy masks on input field to US phone format jquery-input-mask-phone-number

    Simple Add jquery-input-mask-phone-number plugin in to your HTML file and call usPhoneFormat method.

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

    Working JSFiddle Link https://jsfiddle.net/1kbat1nb/

    NPM Reference URL https://www.npmjs.com/package/jquery-input-mask-phone-number

    GitHub Reference URL https://github.com/rajaramtt/jquery-input-mask-phone-number

    0 讨论(0)
  • 2020-11-28 07:46

    Using jQuery Mask Plugin there is two possible ways to implement it:

    1- Following Anatel's recomendations: https://gist.github.com/3724610/5003f97804ea1e62a3182e21c3b0d3ae3b657dd9

    2- Or without following Anatel's recomendations: https://gist.github.com/igorescobar/5327820

    All examples above was coded using jQuery Mask Plugin and it can be downloaded at: http://igorescobar.github.io/jQuery-Mask-Plugin/

    0 讨论(0)
  • 2020-11-28 07:56

    Here is a jQuery phone number mask. No plugin required. Format can be adjusted to your needs.

    Updated JSFiddle.

    HTML

    <form id="example-form" name="my-form">
        <input id="phone-number" name="phone-number" type="text" placeholder="(XXX) XXX-XXXX">
    </form>
    

    JavaScript

    $('#phone-number', '#example-form')
    
    .keydown(function (e) {
        var key = e.which || e.charCode || e.keyCode || 0;
        $phone = $(this);
    
        // Don't let them remove the starting '('
        if ($phone.val().length === 1 && (key === 8 || key === 46)) {
            $phone.val('('); 
            return false;
        } 
        // Reset if they highlight and type over first char.
        else if ($phone.val().charAt(0) !== '(') {
            $phone.val('('+$phone.val()); 
        }
    
        // Auto-format- do not expose the mask as the user begins to type
        if (key !== 8 && key !== 9) {
            if ($phone.val().length === 4) {
                $phone.val($phone.val() + ')');
            }
            if ($phone.val().length === 5) {
                $phone.val($phone.val() + ' ');
            }           
            if ($phone.val().length === 9) {
                $phone.val($phone.val() + '-');
            }
        }
    
        // Allow numeric (and tab, backspace, delete) keys only
        return (key == 8 || 
                key == 9 ||
                key == 46 ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105)); 
    })
    
    .bind('focus click', function () {
        $phone = $(this);
    
        if ($phone.val().length === 0) {
            $phone.val('(');
        }
        else {
            var val = $phone.val();
            $phone.val('').val(val); // Ensure cursor remains at the end
        }
    })
    
    .blur(function () {
        $phone = $(this);
    
        if ($phone.val() === '(') {
            $phone.val('');
        }
    });
    
    0 讨论(0)
提交回复
热议问题