- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在处理一个要求,我只想允许偶数到文本框或数字框(输入类型数字)。最小和最大限制从 4 到 14,如果我们有数字框,它应该只增加 2。
我尝试使用具有 min max 和 step 属性的 HTML 输入类型编号,它工作正常,但我们可以使用任何数字编辑文本框以限制我尝试使用指令,但它不适合我。如果有人能帮我解决这个问题,我会很高兴。
HTML :
<body ng-controller="ctrl">
new : <number-only-input step="2" min="4" max="14" input-value="wks.number" input-name="wks.name" >
</body>
脚本:
var app = angular.module('app', []);
app.controller('ctrl', function($scope){
$scope.name = 'Samir Shah';
$scope.price = -10;
$scope.wks = {number: '', name: 'testing'};
});
app.directive('numberOnlyInput', function () {
return {
restrict: 'EA',
template: '<input type="text" name="{{inputName}}" ng-model="inputValue" />',
scope: {
inputValue: '=',
inputName: '=',
min: '@',
max: '@',
step: '@'
},
link: function (scope) {
scope.$watch('inputValue', function(newValue,oldValue) {
var arr = String(newValue).split("");
if (arr.length === 0) return;
if (arr.length === 1 && (arr[0] == '-' || arr[0] === '.' )) return;
if (arr.length === 2 && newValue === '-.') return;
if (isNaN(newValue)) {
scope.inputValue = oldValue;
return;
}
if(!isNaN(newValue)){
if(newValue < parseInt(scope.min) || newValue > parseInt(scope.max)){
scope.inputValue = oldValue;
return;
}
}
});
}
};
});
最佳答案
<form name="testForm">
<div ng-controller="MyCtrl">
<input type="text" name="testInput" ng-model="number" ng-min="2" ng-max="14" required="required" numbers-only="numbers-only" />
<div ng-show="testForm.testInput.$error.nonnumeric" style="color: red;">
Numeric input only.
</div>
<div ng-show="testForm.testInput.$error.belowminimum" style="color: red;">
Number is too small.
</div>
<div ng-show="testForm.testInput.$error.abovemaximum" style="color: red;">
Number is too big.
</div>
<div ng-show="testForm.testInput.$error.odd" style="color: red;">
Numeric is odd.
</div>
</div>
</form>
angular.module('myApp', []).directive('numbersOnly', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, modelCtrl) {
element.bind('blur', function () {
if (parseInt(element.val(), 10) < attrs.ngMin) {
modelCtrl.$setValidity('belowminimum', false);
scope.$apply(function () {
element.val('');
});
}
});
modelCtrl.$parsers.push(function (inputValue) {
// this next if is necessary for when using ng-required on your input.
// In such cases, when a letter is typed first, this parser will be called
// again, and the 2nd time, the value will be undefined
if (inputValue == undefined) return ''
var transformedInput = inputValue.replace(/[^0-9]/g, '');
if (transformedInput != inputValue || (parseInt(transformedInput, 10) < parseInt(attrs.ngMin, 10) && transformedInput !== '1') || parseInt(transformedInput, 10) > parseInt(attrs.ngMax, 10) || (transformedInput % 2 !== 0 && transformedInput !== '1')) {
if (transformedInput != inputValue) {
modelCtrl.$setValidity('nonnumeric', false);
} else {
modelCtrl.$setValidity('nonnumeric', true);
}
if (parseInt(transformedInput, 10) < parseInt(attrs.ngMin, 10) && transformedInput !== '1') {
modelCtrl.$setValidity('belowminimum', false);
} else {
modelCtrl.$setValidity('belowminimum', true);
}
if (parseInt(transformedInput, 10) > parseInt(attrs.ngMax, 10)) {
modelCtrl.$setValidity('abovemaximum', false);
} else {
modelCtrl.$setValidity('abovemaximum', true);
}
if (transformedInput % 2 !== 0 && transformedInput !== '1') {
modelCtrl.$setValidity('odd', false);
} else {
modelCtrl.$setValidity('odd', true);
}
transformedInput = '';
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
return transformedInput;
}
modelCtrl.$setValidity('nonnumeric', true);
modelCtrl.$setValidity('belowminimum', true);
modelCtrl.$setValidity('abovemaximum', true);
modelCtrl.$setValidity('odd', true);
return transformedInput;
});
}
};
});
活跃的 fiddle http://jsfiddle.net/tuckerjt07/1Ldmkmog/
关于 Angular : how to restrict input type number to allow only even number with min and max limit as well as steps to increase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31000193/
我正在尝试模拟 Max-Min 和 Min-Min 调度算法,并在模拟中自己编写代码。但是不太了解如何在代码中实现它们的工作方式。 例如,在 FCFS 算法中我使用了 3 个服务器 (vms),每个服
有人可以帮我实现这个功能吗?此功能位于相机应用程序内部,该应用程序使用过滤算法来检测颜色变化等方面的差异。语法对我来说非常困难。我不知道如何处理参数中的指针、最小和最大变量语法、什么是增量等?有人可以
我遇到如图所示的表数据情况,我想从每个唯一成员中选择 min(code) 和 secondary_min(code) 。 即期望的输出看起来像 member | min(code) | s
我有一个查询,选择每小时的最小值: SELECT MIN(price), HOUR(timestamp), DATE(timestamp) FROM `scan` GROUP BY DATE(time
#include int min(int pArray[], int nrOfArrayElements) { min = pArray[0]; for (int i = 1; i
generate(vec.begin(), vec.end(), [=](){return static_cast(static_cast(ran()) /RAND_MAX*(max-min)+min
当 min 已经被定义为宏时,如何调用 std::min? 最佳答案 (std::min)(x,y) min 周围的括号防止宏扩展。这适用于所有函数宏。 关于c++ - 当 min 被定义为宏时如何调
我正在尝试对(几个)SQL 数据库中的现有数据负载进行一些转换分析。 数据结构本身非常简单。它只是一个 Actor 列表(比如 user_id)和他们所做的事情的名称。它看起来像这样(还有其他数据,但
我正在尝试根据浏览器的最小高度和最小宽度更改我页面上的 CSS,所以我正在使用它: @media (min-height: 500px), (min-width: 580px) { /* CSS
我有两张 table 。第一个表显示 id_product 和 Product_price_value。下面我将向您展示一个示例(在我的数据库中有很多行) 表:主产品 ID_product: prod
我有两个表:商品和价格(一对多) 每个项目都有一个默认价格,但是这个价格可以在第二个表中被覆盖(在某些情况下)。 首先,我在获取所有项目并预先计算最低价格 - 默认价格与其覆盖当前价格(如果有的话?)
我使用以下命令用 pandas 读取了此 Excel 工作表(仅“DATEHEUREMAX”列): xdata = read_excel('Data.xlsx', 'Data', usecols=['
我想了解min-max堆删除的过程是如何工作的,我已经搜索了它的伪代码但一无所获,而且我似乎不能在这里询问伪代码。所以这是我的问题 谁能展示“删除最小元素 7”的逻辑,至少让我知道伪代码“感觉如何”?
将 std::min 传递给函数不会编译。我将 std::min 的 libcpp 声明复制到我的源文件中并且它有效。 std 版本有什么问题? clang 和 gcc 也是如此。在 Godbolt
请看这个例子:http://jsfiddle.net/vrgT3/5/ 我用 overflow: auto; 创建了一个 250x250px 父 div,因此当内容溢出框时会出现滚动条。我设置了蓝色背
假设我有 4 个变量 a、b、x、y和一个约束 min(a,b) > min(x,y)。 我如何在 pulp python 中表示这个程序? 最佳答案 好的。所以,我发布(删除)的第一个答案有点仓促,
我刚刚经历了 THIS fiddle 和代码如下所示: 现在,当我使用 View 框并将值更改为 viewbox="100 100 225 225" 时它具有执行以下操作的效果
我有 minSdkVersion 16,我想搜索正确的支持库以便使用方法 setActionBar()(在 api 级别 21 中引入)。 我应该使用哪个 appcompat 版本?当然,我不想使用旧
bootstrap.min.css 和 bootstrap.min.js 有什么区别?为什么需要包含 bootstrap.min.js? 和 最佳答案 它们都是完整 Bootstrap 样式 (C
我是一名优秀的程序员,十分优秀!