可以编写angularjs的自定义指令来实现验证文本框填入的数值是来为小数。
airExpressApp.directive(‘validateDecimalCharacters‘, function () { var REQUIRED_PATTERNS = [ /^-?[0-9]\d*(\.\d+)?$/ ]; return { require: ‘ngModel‘, link: function ($scope, element, attributes, ngModelController) { ngModelController.$validators.validateDecimalCharacters = function (modelValue, viewValue) { var status = true; if (!ngModelController.$isEmpty(modelValue)) { angular.forEach(REQUIRED_PATTERNS, function (pattern) { status = status && pattern.test(modelValue); }); } return status; }; } } });
Source Code
演示:
时间: 2024-10-25 18:22:48