如何限制输入编号以非十进制 [英] How to restrict input number to non-decimal

查看:140
本文介绍了如何限制输入编号以非十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面上的号码输入,我想它仅限于整数值,没有小数。我使用验证的形式和angularjs。我有什么做的,使小数不准?我试着设置步骤1,但是这什么都没有改变。许多其他的问题,讨论如何浏览器,支持小数 - 我似乎有相反的问题。

I have a number input on a page, and I want it restricted to whole number values, no decimals. I am using forms and angularjs for validation. What do I have to do to make decimals not allowed? I tried setting step to 1, but this changed nothing. Many other issues discuss getting browsers to support decimals - I seem to be having the opposite problem.

推荐答案

您可以为一个自定义的指令,纳入的 ngModelController。
像这样:概念验证

You can make a custom directive for that, incorporating the ngModelController. Like this: Proof of Concept

    module.directive("noDecimalInput", function() {
    return {
        restrict: "A",
        require:"ngModel",
        link: function(scope,element,attr,ctrl) {
            ctrl.$parsers.push(function(value){
                if(value.indexOf(".")>-1) {
                    ctrl.$setValidity("notDecimal",false);
                    return undefined;
                }
                ctrl.$setValidity("notDecimal",true);
                return value;
            });
        }
    }
});

这篇关于如何限制输入编号以非十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆