Nid*_*mar 5 html javascript regex angularjs ionic-framework
我正在使用允许多个小数点的类型 number 输入,所以我尝试使用正则表达式不允许超过一个小数点但即使在使用正则表达式后我也面临同样的问题谁能告诉我如何只允许ionic1 中类型数字输入中的一个小数点
网址:
<input stopccp focus-me class="inputContainer trade_input" type="number" name="" ng-model="vm.total_amount[$index]" ng-change="vm.onTotalCost()" limit-char limit="5" ng-keyup="vm.decimalcheck(vm.total_amount[$index])" >
Run Code Online (Sandbox Code Playgroud)
函数中的正则表达式:
function decimalcheck (element) {
$log.log('decimalcheck got called', element);
var regex = /\d*\.?\d?/g;
return regex.exec(element);
}
Run Code Online (Sandbox Code Playgroud)
试试这个正则表达式:
^\d*\.?\d+$
Run Code Online (Sandbox Code Playgroud)
解释:
^- 字符串的开头\d*- 0+ 位数字\.?- 0 或 1 个小数点\d+- 1+ 位数字(因此,小数点后必须至少有一位数字(如果有)$- 字符串结尾