Javascript 从值中仅获取数字,并从文本字段中计算长度
我有一个文本字段Javascript 从值中仅获取数字,并从文本字段中计算长度,javascript,jquery,Javascript,Jquery,我有一个文本字段 <input id="prime-phone" class="number-format" name="prime-phone" placeholder="" type="tel"> 数字格式可以更改,如“(432)432-2432”,“(43)-(432)-2432” 谢谢 提前获取帮助:)使用正则表达式匹配数字和计数: sd.match(/\d/g).length 使用正则表达式匹配数字和计数: sd.match(/\d/g).length 使用正则表达式
<input id="prime-phone" class="number-format" name="prime-phone" placeholder="" type="tel">
数字格式可以更改,如“(432)432-2432”
,“(43)-(432)-2432”
谢谢
提前获取帮助:)使用正则表达式匹配数字和计数:
sd.match(/\d/g).length
使用正则表达式匹配数字和计数:
sd.match(/\d/g).length
使用正则表达式匹配数字和计数:
sd.match(/\d/g).length
使用正则表达式匹配数字和计数:
sd.match(/\d/g).length
试试看
试试看
试试看
尝试使用和计数删除非数字字符
sd=$(".number-format").val();
alert(sd);
len=sd.replace(/\D/g,'').length;
// \D used to match all non-digit character
alert(len);
使用和计数删除非数字字符
sd=$(".number-format").val();
alert(sd);
len=sd.replace(/\D/g,'').length;
// \D used to match all non-digit character
alert(len);
使用和计数删除非数字字符
sd=$(".number-format").val();
alert(sd);
len=sd.replace(/\D/g,'').length;
// \D used to match all non-digit character
alert(len);
使用和计数删除非数字字符
sd=$(".number-format").val();
alert(sd);
len=sd.replace(/\D/g,'').length;
// \D used to match all non-digit character
alert(len);
可重复使用的解决方案。
一旦定义好,你就可以调用它们来排除不需要的字符并获得长度。我发现这更容易维护
var tel = "(432)432-2432";
var getLength = function(tel){
var telStr = tel.split('');
var excpetionList = ['(',')','-']; //<-- here you can add the chars to remove
return telStr.filter(function(item,index,arr){
if(excpetionList.indexOf(item) < 0){
return item;
}
}).join('').length;
};
console.log(getLength(tel));
var tel=“(432)432-2432”;
var getLength=函数(电话){
var telStr=tel.split(“”);
var expetionlist=['(',')','-'];//一个可重用的解决方案。
一旦定义好,你就可以调用它们来排除不需要的字符并获得长度。我发现这更容易维护
var tel = "(432)432-2432";
var getLength = function(tel){
var telStr = tel.split('');
var excpetionList = ['(',')','-']; //<-- here you can add the chars to remove
return telStr.filter(function(item,index,arr){
if(excpetionList.indexOf(item) < 0){
return item;
}
}).join('').length;
};
console.log(getLength(tel));
var tel=“(432)432-2432”;
var getLength=函数(电话){
var telStr=tel.split(“”);
var expetionlist=['(',')','-'];//一个可重用的解决方案。
一旦定义好,你就可以调用它们来排除不需要的字符并获得长度。我发现这更容易维护
var tel = "(432)432-2432";
var getLength = function(tel){
var telStr = tel.split('');
var excpetionList = ['(',')','-']; //<-- here you can add the chars to remove
return telStr.filter(function(item,index,arr){
if(excpetionList.indexOf(item) < 0){
return item;
}
}).join('').length;
};
console.log(getLength(tel));
var tel=“(432)432-2432”;
var getLength=函数(电话){
var telStr=tel.split(“”);
var expetionlist=['(',')','-'];//一个可重用的解决方案。
一旦定义好,你就可以调用它们来排除不需要的字符并获得长度。我发现这更容易维护
var tel = "(432)432-2432";
var getLength = function(tel){
var telStr = tel.split('');
var excpetionList = ['(',')','-']; //<-- here you can add the chars to remove
return telStr.filter(function(item,index,arr){
if(excpetionList.indexOf(item) < 0){
return item;
}
}).join('').length;
};
console.log(getLength(tel));
var tel=“(432)432-2432”;
var getLength=函数(电话){
var telStr=tel.split(“”);
变量expetionList=['(',')','-']//