我在Angular JS中为银行应用程序的帐号进行数字格式化。
如何实施帐户格式11-02980-1。我希望在有些添加数字(11029801)时将分隔符( - )自动添加到输入文本中。即当我输入11时,添加分隔符( - ),然后添加02980,添加另一个分隔符,最后添加1
由于
答案 0 :(得分:0)
修改强>
这是你在找什么?
试试这个代码段:
var MyApp = angular.module('MyApp', []);
MyApp.controller('TestController', TestController);
function TestController($scope) {
$scope.accNumber = '';
$scope.previousAccNumber = '';
$scope.editAccountNumber = function (accNumber) {
if ($scope.previousAccNumber.length > accNumber.length) {
$scope.previousAccNumber = accNumber;
return accNumber;
}
accNumber = accNumber.replace(/-/g, '');
var classA = accNumber.slice(0, 2);
var classB = accNumber.slice(2, 7);
var classC = accNumber.slice(7, 8);
var finalNumber = [];
if (classA && classA.length == 2) {
finalNumber.push(classA, "-");
} else {
return classA;
}
if (classB && classB.length == 5) {
finalNumber.push(classB, "-");
} else {
return classA + "-" + classB;
}
if (classC && classC.length == 1) {
finalNumber.push(classC);
}
$scope.previousAccNumber = finalNumber.join('');
return finalNumber.join('');
}
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<div ng-controller="TestController">
<input id="accNumber" type="text" placeholder="Enter Account Number" ng-model="accNumber" ng-change="accNumber = editAccountNumber(accNumber)" />
</div>
</div>
&#13;