2017-10-10 32 views
0

JavaScriptを使用してフィールドにマスクを配置する必要があります。例: +55(11)98888-0000または+55(11)8888-0000。マスカラ入力ジャバスクリプト番号国番号

私はこの(11)99999-9999または(11)9999-9999のように動作するjavascript関数を持っています。上記の例のように変更する必要があります。 ここに機能コードがあります。

<!doctype html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <meta name="Generator" content="EditPlus®"> 
 
    <meta name="Author" content=""> 
 
    <meta name="Keywords" content=""> 
 
    <meta name="Description" content=""> 
 
    <title>Mascara Telefone</title> 
 
</head> 
 
<body> 
 
    <html> 
 
<head> 
 
    <title>Mascara Telefone</title> 
 
<script type="text/javascript"> 
 
/* Máscaras ER */ 
 
function mascara(o,f){ 
 
    v_obj=o 
 
    v_fun=f 
 
    setTimeout("execmascara()",1) 
 
} 
 
function execmascara(){ 
 
    v_obj.value=v_fun(v_obj.value) 
 
} 
 
function mtel(v){ 
 
    v=v.replace(/\D/g,"");     // Removes all non-digit 
 
    v=v.replace(/^(\d{2})(\d)/g,"($1) $2"); // Place parentheses around the first two digits 
 
    v=v.replace(/(\d)(\d{4})$/,"$1-$2"); // Places hyphen between the fourth and fifth digits 
 
    return v; 
 
} 
 
function id(el){ 
 
\t return document.getElementById(el); 
 
} 
 
window.onload = function(){ 
 
\t id('telefone').onkeyup = function(){ 
 
\t \t mascara(this, mtel); 
 
\t } 
 
} 
 
</script> 
 

 
</head> 
 
<body> 
 
    <input type="text" name="telefone" id="telefone" maxlength="15" /> 
 

 
</body> 
 
</html> 
 
</body> 
 
</html>

答えて

0

あなたは+55(11)8888から0000までのexp V以下この使用のために機能

への入力のテキストの長さに基づいて条件を指定することができます(\ d {2})(\ d {4})(\ d)/ g、 "+" + "$ 1($ 2)$ 3- $ 4"); max \ lenth = 18

for +55(11)98888-0000以下を使用してください。 v = v.replace(/ ^(\ d {2})(\ d {2})(\ d {2} })(\ D)/ gで、 "+" + "$ 1($ 2)$ 3- $ 4")、および最大長= 19

<!doctype html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <meta name="Generator" content="EditPlus®"> 
 
    <meta name="Author" content=""> 
 
    <meta name="Keywords" content=""> 
 
    <meta name="Description" content=""> 
 
    <title>Mascara Telefone</title> 
 
</head> 
 
<body> 
 
    <html> 
 
<head> 
 
    <title>Mascara Telefone</title> 
 
<script type="text/javascript"> 
 
/* Máscaras ER */ 
 
function mascara(o,f){ 
 
    v_obj=o 
 
    v_fun=f 
 
    setTimeout("execmascara()",1) 
 
} 
 
function execmascara(){ 
 
    v_obj.value=v_fun(v_obj.value) 
 
} 
 
function mtel(v){ 
 
    v=v.replace(/\D/g,"");     // Removes all non-digit 
 
    v=v.replace(/^(\d{2})(\d{2})(\d{5})(\d)/g,"+"+"$1 ($2) $3-$4"); 
 
    return v; 
 
} 
 
function id(el){ 
 
\t return document.getElementById(el); 
 
} 
 
window.onload = function(){ 
 
\t id('telefone').onkeyup = function(){ 
 
\t \t mascara(this, mtel); 
 
\t } 
 
} 
 
</script> 
 

 
</head> 
 
<body> 
 
    <input type="text" name="telefone" id="telefone" maxlength="19" /> 
 

 
</body> 
 
</html> 
 
</body> 
 
</html>

関連する問題