简体   繁体   English

如何使用输入type = text字段仅接受数字,减号和小数,而Angular仅接受

[英]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... 问题:这很好,但是...

  1. The value in the text box starts out with the number "1"; 文本框中的值以数字“ 1”开头; why? 为什么?
  2. The text box will 'not' allow dashes, decimals with the numbers but, it does not allow alpha characters. 文本框将“不允许”使用破折号,带数字的小数,但不允许使用字母字符。 Good. 好。
  3. This is for LAT/LON data restricted to 50 characters which does work as maxlength='50' 这适用于限制为50个字符的LAT / LON数据,其最大长度为'50'
  4. What does the "=" sign mean at the end of the inputValue and inputName? inputValue和inputName末尾的“ =”符号是什么意思? Is that a standard? 这是标准吗? because when I change it to, say a "0", thinking that's why the "1" appears in the text box, the debugger in Chrome shows code failures. 因为当我将其更改为“ 0”时,认为这就是为什么“ 1”出现在文本框中的原因,Chrome中的调试器显示了代码故障。

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. 感谢大家。

1. comes from input-value="wksLon.number" 1.来自input-value="wksLon.number"

2. match your value to regex 2.使您的价值与正则表达式匹配

js JS

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将其更改为您的设置

something like that 这样的东西

scope.$watch('inputValue', function (newValue, oldValue) {
  var myRegex = /^(-)?\d+(.\d+)$/
  if (!myRegex.test(newValue) || newValue.length > 50) {
    scope.inputValue = oldValue
  }
});

3. extended 2 by 3 with length > 50 3. length > 50扩展2 3

4. that's a data binding to same name 4.那是一个数据绑定到同一个名字

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM