javascript 带有正则表达式的文本字段的Javascript电话掩码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15802100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:09:33  来源:igfitidea点击:

Javascript phone mask for text field with regex

javascriptregexinputstring-formattingmask

提问by Danilo

I'm using this function to phone mask and works almost perfectly.

我正在使用此功能进行电话屏蔽,并且几乎可以完美运行。

function mask(o, f) 
{ 
    v_obj = o; 
    v_fun = f; 
    setTimeout("execmask()", 1) 
};

function execmask() 
{ 
    v_obj.value = v_fun(v_obj.value) 
};

function mphone(v){
    v=v.replace(/\D/g,"");           
    v=v.substring(0, 11);
    v=v.replace(/^(\d{2})(\d)/g,"(OXX) "); 
    v=v.replace(/(\d)(\d{4})$/,"-"); 
    return v;
}

Here I run the mask in the text field:

在这里,我在文本字段中运行掩码:

<input type="text" id="phone" name="phone" onkeypress="mask(this, mphone);" onblur="mask(this, mphone);" />

The problem is that I need to change this part of the code (OXX$1)for (0XX$1).

问题是我需要将这部分代码(OXX$1)更改为(0XX$1)

Current situation with 9 digit: (OXX99) 99999-9999.

9 位当前情况:(OXX99) 99999-9999

Current situation with 8 digit: (OXX99) 9999-9999.

8 位数字的现状:(OXX99) 9999-9999

The correct formatting that I need with 9 digit: (0XX99) 99999-9999

我需要的 9 位正确格式:(0XX99) 99999-9999

The correct formatting that I need with 8 digit: (0XX99) 99999-9999

我需要的 8 位正确格式:(0XX99) 99999-9999

The amount of 8 or 9 digit is the choice of the user.

8位或9位的数量由用户选择。

If I change the character 'O' for '0', causes an error in the mask.

如果我将字符 'O' 更改为 '0',则会导致掩码出错。

Please, help!

请帮忙!

回答by Markus Jarderot

function mask(o, f) {
    setTimeout(function () {
        var v = f(o.value);
        if (v != o.value) {
            o.value = v;
        }
    }, 1);
}

function mphone(v) {
    var r = v.replace(/\D/g,"");
    r = r.replace(/^0/,"");
    if (r.length > 10) {
        // 11+ digits. Format as 5+4.
        r = r.replace(/^(\d\d)(\d{5})(\d{4}).*/,"(0XX) -");
    }
    else if (r.length > 5) {
        // 6..10 digits. Format as 4+4
        r = r.replace(/^(\d\d)(\d{4})(\d{0,4}).*/,"(0XX) -");
    }
    else if (r.length > 2) {
        // 3..5 digits. Add (0XX..)
        r = r.replace(/^(\d\d)(\d{0,5})/,"(0XX) ");
    }
    else {
        // 0..2 digits. Just add (0XX
        r = r.replace(/^(\d*)/, "(0XX");
    }
    return r;
}

http://jsfiddle.net/BBeWN/

http://jsfiddle.net/BBeWN/

回答by Nicolas Giszpenc

I love this function and I use it all the time. I've added 2 other masks if anyone needs them. I understand that they don't directly answer the question, but they are super useful.

我喜欢这个功能,我一直在使用它。如果有人需要,我已经添加了另外两个面具。我知道他们不会直接回答问题,但它们非常有用。

//Social Security Number for USA
 function mssn(v) {
    var r = v.replace(/\D/g,"");
    r = r.replace(/^0/,"");
    if (r.length > 9) {
        r = r.replace(/^(\d\d\d)(\d{2})(\d{0,4}).*/,"--");
        return r;
    }
    else if (r.length > 4) {
        r = r.replace(/^(\d\d\d)(\d{2})(\d{0,4}).*/,"--");
    }
    else if (r.length > 2) {
        r = r.replace(/^(\d\d\d)(\d{0,3})/,"-");
    }
    else {
        r = r.replace(/^(\d*)/, "");
    }
    return r;
}

//USA date
function mdate(v) {
   var r = v.replace(/\D/g,"");
   if (r.length > 4) {
    r = r.replace(/^(\d\d)(\d{2})(\d{0,4}).*/,"//");
   }
   else if (r.length > 2) {
    r = r.replace(/^(\d\d)(\d{0,2})/,"/");
   }
   else if (r.length > 0){
         if (r > 12) {
           r = "";
         }
   }
   return r;
}