嗨,我是 angularjs 的新手,我们想要实现一个输入文本框,该文本框仅允许六位数字,输入六位数字后,逗号分隔将添加并输入另外六位数字。你能帮我么。我们现在的示例代码是
**html**
<div ng-controller="MyCtrl">
<input type="text" ng-model="number" required="required" numbers-
only="numbers-only" />
</div>
Angular js Controller
angular.module('myApp', []).directive('numbersOnly', function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
if (inputValue == undefined) return ''
var transformedInput = inputValue.replace(/[^0-9]/g, '');
if (transformedInput!=inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
function MyCtrl($scope) {
$scope.number = ''
}
我们找到的 fiddler 链接
最佳答案
看看ui-mask ,将掩码设置为 999999-999999
或者您也可以使用 angular-input-mask
关于javascript - 输入仅允许 6 位数字,在逗号分隔后允许在 Angular js 中输入另外 6 位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29227213/