简体   繁体   English

仅一个点“。”正则表达式

[英]Only one dot “.” regex

I need to be able to set an input in order to put only numbers and one dot, like 111.111 and not 111..110....11 or 11.11.11.11 . 我需要能够设置输入以仅放置数字和一个点,例如111.111而不是111..110....1111.11.11.11

This is because I am doing a calculation, and if the user enters more than one dot in the same input, then the calculation stops, I have this in a directive 这是因为我正在执行计算,并且如果用户在同一输入中输入多个点,则计算将停止,我将其保存在指令中

.directive('numbersOnly', function () {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function (scope, element, attrs, ngModelCtrl) {
      ngModelCtrl.$parsers.push(function (inputValue) {
        var transformedInput = inputValue.replace(/[^0-9\.]+/g, '');
        if (transformedInput !== inputValue) {
          ngModelCtrl.$setViewValue(transformedInput);
          ngModelCtrl.$render();
        }
        return transformedInput;
      });
    }
  }
})

so what should I do here 那我该怎么办

var transformedInput = inputValue.replace(/[^0-9\.]+/g, '');

?

UPDATE 更新

here is the code where I am trying to use it: https://jsfiddle.net/dado1ynj/4/ 这是我尝试使用它的代码: https : //jsfiddle.net/dado1ynj/4/

Either use a lookahead: 请先行使用:

^(?!.*\..*\.)(\d+\.\d+)

Demo 演示版

Or anchors: 或锚点:

^(\d+\.\d+)$

Demo2 演示2

Well, it depends about what is the format that you will accept. 好吧,这取决于您将接受哪种格式。 Using /^(\\d) (.)?(\\d) $/ will help you to ensure that there is only one point in the number, but also accepts numbers without decimal point or just the decimal point. 使用/ ^(\\ d) (。)?(\\ d) $ /可以帮助您确保数字中只有一个点,而且可以接受不带小数点或仅带小数点的数字。 Also you can use isNaN() to make sure it is a number. 您也可以使用isNaN()来确保它是一个数字。

Hope it works for you. 希望对你有效。

I would suggest just testing for more than 1 dot. 我建议仅测试1个以上的点。 Example

(string.test(/\d\.\.?(\.+)/gm)) ? //do true : // do false;

test checks if it exists in string and then returns true or false. test检查它是否存在于字符串中,然后返回true或false。

the regex checks for a digit then two dots or two dots +. 正则表达式先检查一个数字,然后是两个点或两个点+。 It checks globally and multiple lines. 它检查全局和多行。

Then if you're insistant on using regex to check for three groups of three digits separated by one dot you can use the regex expression 然后,如果您坚持使用正则表达式来检查三组由一点分隔的三位数,则可以使用正则表达式

/(\d{3}\.\d{3}\.\d{3})\b/gm

Example here: http://regexr.com/3auj5 此处的示例: http : //regexr.com/3auj5

Not the most elegant solution, but it checks the input as the user types and only allows a number with a single decimal. 这不是最优雅的解决方案,但是它会在用户输入时检查输入内容,并且只允许使用带小数点的数字。

 link: function (scope, element, attrs, ngModelCtrl) {
        scope.currentValue = '';
        element.bind('keyup',function(e) {
            if( e.which!=8 && e.which!=0 && e.which!=46 && e.which != 190 && (e.which<48 || e.which>57)) {
                element.val(scope.currentValue);
            }
            if(e.which != 8 && e.which != 0) {
                var valToCheck = element.val();

                var r = /^(\d*)\.{0,1}(\d*)$/
                if (!r.test(valToCheck)) {
                    element.val(scope.currentValue);
                } else {
                    scope.currentValue = element.val();
                }

            }


        });

The problem with this is that you see the user type in a letter, for example, and then you see it disappear. 问题在于,例如,您看到用户键入字母,然后看到它消失了。 This works much better using "keypress" rather than "keyup" however then you have to take into account the user clicking the mouse somewhere in the middle of the currently inputted value and then typing so it gets tricky. 使用“ keypress”而不是“ keyup”会更好,但是您必须考虑到用户在当前输入值中间的某个位置单击鼠标,然后键入鼠标,这样比较棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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