[英]How to use an input type=text field to accept only digits, minus sign and decimals and nothing more with Angular
I'll be brief and to the point: I have two text boxes with angular. 我要简短一点:我有两个带角度的文本框。
Here it is: 这里是:
<div data-ng-controller="customValidationAndNbrsCheckController">
<label class="input">
<number-only-input placeholder="Latitude" input-value="wksLat.number" input-name="wksLat.name" />
</label>
<label>
<number-only-input placeholder="Longitude" input-value="wksLon.number" input-name="wksLon.name" />
</label>
</div>
Here's my directive: UPDATE: PROBLEM SOLVED... here's the fiddle that shows the EXACT change that fixes the problem. 这是我的指令:更新:问题已解决...这是显示正确的更改以解决问题的小提琴。 Thanks for reading but I got it. 感谢您的阅读,但我明白了。 http://jsfiddle.net/vfsHX/ http://jsfiddle.net/vfsHX/
//Numbers only function
angular
.module("isNumber", [])
.directive('isNumber', function () {
return {
require: 'ngModel',
link: function (scope) {
scope.$watch('wksLat.number', 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.wksLat.number = oldValue;
}
});
scope.$watch('wksLon.number', 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.wksLon.number = oldValue;
}
});
}
};
});
Here's my Service: 这是我的服务:
(function (app) {
debugger;
var customValidationAndNbrsService = function () {
var customValidationAndNbrsServiceFactory = {};
customValidationAndNbrsServiceFactory.settings = new mainApp.Models.Settings();
customValidationAndNbrsServiceFactory.getSettings = function () {
return customValidationAndNbrsServiceFactory.settings;
};
return customValidationAndNbrsServiceFactory;
};
app.factory("customValidationAndNbrsService", customValidationAndNbrsService);
}(angular.module("mainApp")));
This is from an example I found here on Stack. 这来自我在Stack上找到的示例。
PROBLEM: This works GREAT but... 问题:这很好,但是...
QUESTION: How do I modify the code to "allow" dashes and decimals, since we're entering the LAT LONs as decimal degrees and not DD:MM:SS. 问题:由于我们将LAT LON输入为小数度而不是DD:MM:SS,因此如何将代码修改为“允许”破折号和小数。
Here's the possible input: 25.2223332 LAT and -45.685464 LON 这是可能的输入:25.2223332 LAT和-45.685464 LON
So I hope the moderators do not delete this question. 因此,我希望主持人不要删除此问题。 It's valid and yes, I need a "fix-my-code" help since that's what I believe this site is for. 这是有效的,是的,我需要“修复我的代码”帮助,因为我认为这是该网站的目的。
Thanks, everyone. 感谢大家。
input-value="wksLon.number"
1.来自input-value="wksLon.number"
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;
}
});
this should be changed to your setup, probably with RegEx 应该使用RegEx将其更改为您的设置
scope.$watch('inputValue', function (newValue, oldValue) {
var myRegex = /^(-)?\d+(.\d+)$/
if (!myRegex.test(newValue) || newValue.length > 50) {
scope.inputValue = oldValue
}
});
length > 50
3. length > 50
扩展2 3 <number-only-input placeholder="Longitude" input-value="wksLon.number" input-name="wksLon.name" />
from above element input-value and input-name are binded with the same name 从上面的元素输入值和输入名称绑定到相同的名称
scope: {
inputValue: '=',
inputName: '='
},
but if you want to use different you can do it like: 但是,如果您想使用其他功能,可以按照以下步骤操作:
scope: {
Value: '=inputValue',
Name: '=inputName'
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.