我在HTML表单中有一个需要格式化的电话字段
(555)555-5555
以下是该字段:
<input type="tel" id="phone" name="phone" placeholder="(555) 555-5555" autocomplete="off" maxlength="14" required="required">
一些要求:
如何使用jQuery / Javascript将这种类型的要求应用于这些要求?
答案 0 :(得分:6)
我拼凑了其他相关问题的答案,以及我自己的代码,并提出了似乎运作良好的解决方案:
// Phone formatting: (555) 555-5555
$("#phone").on("keyup paste", function() {
// Remove invalid chars from the input
var input = this.value.replace(/[^0-9\(\)\s\-]/g, "");
var inputlen = input.length;
// Get just the numbers in the input
var numbers = this.value.replace(/\D/g,'');
var numberslen = numbers.length;
// Value to store the masked input
var newval = "";
// Loop through the existing numbers and apply the mask
for(var i=0;i<numberslen;i++){
if(i==0) newval="("+numbers[i];
else if(i==3) newval+=") "+numbers[i];
else if(i==6) newval+="-"+numbers[i];
else newval+=numbers[i];
}
// Re-add the non-digit characters to the end of the input that the user entered and that match the mask.
if(inputlen>=1&&numberslen==0&&input[0]=="(") newval="(";
else if(inputlen>=6&&numberslen==3&&input[4]==")"&&input[5]==" ") newval+=") ";
else if(inputlen>=5&&numberslen==3&&input[4]==")") newval+=")";
else if(inputlen>=6&&numberslen==3&&input[5]==" ") newval+=" ";
else if(inputlen>=10&&numberslen==6&&input[9]=="-") newval+="-";
$(this).val(newval.substring(0,14));
});
答案 1 :(得分:0)
你也可以使用这个小方法:
$("#phone").keydown(function(e) {
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 3) {
if( e.keyCode!=8 ){
$(this).val("(" + curval + ")" + " ");
}
} else if (curchr == 9) {
if( e.keyCode!=8 ){
$(this).val(curval + "-");
}
}
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105) && e.keyCode !=8 ) {
e.preventDefault();
}
});
答案 2 :(得分:0)
我非常喜欢Nick Petrie的回答,但我认为结束括号&#34;)&#34;输入位置3中的数字时应添加,而不是位置4中的数字(当前的数字)。
以下是修改后的版本:
// Phone formatting: (555) 555-5555
$("#Phone").on("keyup paste", function(event) {
// Don't run for backspace key entry, otherwise it bugs out
if(event.which != 8){
// Remove invalid chars from the input
var input = this.value.replace(/[^0-9\(\)\s\-]/g, "");
var inputlen = input.length;
// Get just the numbers in the input
var numbers = this.value.replace(/\D/g,'');
var numberslen = numbers.length;
// Value to store the masked input
var newval = "";
// Loop through the existing numbers and apply the mask
for(var i=0;i<numberslen;i++){
if(i==0) newval="("+numbers[i];
else if(i==2) newval+=numbers[i]+") ";
else if(i==6) newval+="-"+numbers[i];
else newval+=numbers[i];
}
// Re-add the non-digit characters to the end of the input that the user entered and that match the mask.
if(inputlen>=1&&numberslen==0&&input[0]=="(") newval="(";
else if(inputlen>=6&&numberslen==3&&input[4]==")"&&input[5]==" ") newval+=") ";
else if(inputlen>=5&&numberslen==3&&input[4]==")") newval+=" ";
else if(inputlen>=6&&numberslen==3&&input[5]==" ") newval+=" ";
else if(inputlen>=10&&numberslen==6&&input[9]=="-") newval+="-";
$(this).val(newval.substring(0,14));
}
});
我已将else if(i==3) newval+=numbers[i]+") ";
更改为else if(i==2) newval+=numbers[i]+") ";
并添加了if(event.which != 8){}
语句来捕获退格键输入(因为否则会卡住)。