带有 jQ​​uery 和屏蔽输入插件的电话屏蔽 [英] Phone mask with jQuery and Masked Input Plugin

查看:18
本文介绍了带有 jQ​​uery 和屏蔽输入插件的电话屏蔽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 jQuery 和 屏蔽输入插件屏蔽电话输入时遇到问题.

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

有两种可能的格式:

  1. (XX)XXXX-XXXX
  2. (XX)XXXXX-XXXX

有没有办法掩盖它接受这两种情况?

Is there any way to mask it accepting both cases?

我试过了:

$("#phone").mask("(99) 9999-9999"); 
$("#telf1").mask("(99) 9999*-9999");    
$("#telf1").mask("(99) 9999?-9999"); 

但它不能如我所愿.

最接近的是 (xx)xxxx-xxxxx.

The closest one was (xx)xxxx-xxxxx.

我想在输入第 10 个数字时得到 (xx)xxxx-xxxx,当我输入第 11 个数字时得到 (xx)xxxxx-xxxx.可以吗?

I would like to get (xx)xxxx-xxxx when I type the 10th number, and (xx)xxxxx-xxxx when I type the 11th. Is it posible?

推荐答案

试试这个 - http://jsfiddle.净/dKRGE/3/

$("#phone").mask("(99) 9999?9-9999");

$("#phone").on("blur", function() {
    var last = $(this).val().substr( $(this).val().indexOf("-") + 1 );

    if( last.length == 3 ) {
        var move = $(this).val().substr( $(this).val().indexOf("-") - 1, 1 );
        var lastfour = move + last;
        var first = $(this).val().substr( 0, 9 );

        $(this).val( first + '-' + lastfour );
    }
});

这篇关于带有 jQ​​uery 和屏蔽输入插件的电话屏蔽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆