AngularJS 输入框数字(金钱)的判断

方法:

  $scope.clearNoNum = function (obj, attr) {
    
    

                //先把非数字的都替换掉,除了数字和.
                obj[attr] = obj[attr].replace(/[^\d.]/g, "");
                //必须保证第一个为数字而不是.
                obj[attr] = obj[attr].replace(/^\./g, "");
                //保证只有出现一个.而没有多个.
                obj[attr] = obj[attr].replace(/\.{2,}/g, "");
               //保证.只出现一次,而不能出现两次以上
                obj[attr] = obj[attr].replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
                //只能输入两个小数

                obj[attr] = obj[attr].replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
                //去掉小数点前面的零
                obj[attr]= obj[attr].replace(/^bai0+\./,'0.');
                obj[attr]= obj[attr].replace(/^0+([0-9])/,'$1');

            }

调用

input.form-control(type='text', name='money',ng-model="goverAffairCtrl.goveraffair.money", ng-keyup="clearNoNum(goverAffairCtrl.goveraffair,'money')",required="required",data-parsley-maxlength='10')

猜你喜欢

转载自blog.csdn.net/weixin_45010894/article/details/109521643