簡體   English   中英

如何只允許數字?

[英]How to allow only digits?

請幫助修復腳本。

的HTML:

<input type="text" ng-model="phone" id="phoneInput" ng-change="apt.changePhoneInput(phone)">

js:

var app = angular.module('app', []);
app.controller('Appctrl', Appctrl, '$scope');
function Appctrl($scope){
  this.changePhoneInput = function(phone) {
    var result = phone;
    console.log('phone', phone);
    result.replace(/[^+0-9\(\)]/gim,'');
    console.log('result', result);
    $scope.phone = result;
  };
}

JSFIDDLE

我只允許輸入數字並跟隨符號:'+','((',')','-'。 但是輸入任何符號后替換將不起作用

您缺少對result變量的分配,在替換了除數字以外的字符之后

result=result.replace(/[^+0-9\(\)-]/gim,'');

更改后,您是控制者,應該像

var app = angular.module('app', []);
app.controller('Appctrl', Appctrl, '$scope');
function Appctrl($scope){
  this.changePhoneInput = function(phone) {
    var result = phone;
    console.log('phone', phone);
    result=result.replace(/[^+0-9\(\)-]/gim,'');
    console.log('result', result);
    $scope.phone = result;
  };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM