我正在尝试创建一个与数字格式xxx-xx-xxxx
匹配的过滤器,即。 123-12-1234。
angular.module('ng').filter('tel', function () {
return function (tel) {
if (!tel) { return ''; }
var value = tel.toString().trim().replace(/^\+/, '');
if (value.match(/[^0-9]/)) {
return tel;
}
var country, city, number;
switch (value.length) {
case 9: // +1PPP####### -> C (PPP) ###-####
country = 1;
city = value.slice(0, 3);
number = value.slice(4);
break;
default:
return tel;
}
if (country == 1) {
country = "";
}
number = number.slice(0, 2) + '-' + number.slice(3);
return (country + " " + city + "-" + number).trim();
};
});
function Ctrl($scope){
$scope.phoneNumber = 111223333;
}
有人可以帮助我吗?
答案 0 :(得分:1)
喜欢这个? http://jsfiddle.net/4P7wD/1/
改变一位数。
number = number.slice(0, 2) + '-' + number.slice(1);
<强>已更新强>
希望这是你所需要的:http://jsfiddle.net/4P7wD/2/
答案 1 :(得分:1)
...
switch (value.length) {
case 9: // +1PPP####### -> C (PPP) ###-####
country = 1;
city = value.slice(0, 3);
number = value.slice(3);
break;
default:
return tel;
}
if (country == 1) {
country = "";
}
number = number.slice(0, 2) + '-' + number.slice(2);
return (city + "-" + number).trim();
...
演示:: fiddle