2016-04-28 40 views
-2

块引用 在高度场的情况下,其服用3的整数值,其正确的,但之后,它必须555.34 甚至我使用取2浮置值,例如步长=“0.01”和最大=“3”,但它的按键后没有3位采取任何浮点值 大段引用输入框未服用浮点值

var app = angular.module('Calc', []); 
 
var inputQuantity = []; 
 
    $(function() { 
 
     $(".form-control").each(function(i) { 
 
     inputQuantity[i]=this.defaultValue; 
 
     $(this).data("idx",i); // save this field's index to access later 
 
     }); 
 
     $(document).on("keypress",".form-control", function (e) { 
 
     var $field = $(this), 
 
      val=this.value+''+String.fromCharCode(e.charCode),pattern; 
 
     if(this.step==0.00) 
 
      pattern=/[^0-9]/ 
 
      else 
 
      pattern=/[^0-9.]/ 
 
     if (val>parseInt(this.max,10)||pattern.test(val)|| (val.match(/\./) && (val.match(/\./g).length>1 || val.replace(/\d+\./,'').length>2))) { 
 
       e.preventDefault(); 
 
     } 
 
     });  
 
    }); 
 
app.controller('Calc_Ctrl', function ($scope, $http) { 
 
    $scope.choices = [{id : 'choice1', l2 : 0, b2 : 0}]; 
 
    $scope.areas = [{id : 'choice2', total : 0}]; 
 

 
    $scope.addNewChoice = function() { 
 
      var newItemNo = $scope.choices.length + 1; 
 
      $scope.choices.push({ 
 
       'id' : 'choice' + newItemNo, l2 : 0, b2 : 0 
 
      }); 
 
    }; 
 
    $scope.removeChoice = function() { 
 
      var lastItem = $scope.choices.length - 1; 
 
      if (lastItem !== 0) { 
 
       $scope.choices.splice(lastItem); 
 
      } 
 
    }; 
 
});
<!DOCTYPE html> 
 
<html> 
 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
 
<script src="newscript.js"></script> 
 

 
<body> 
 
    <div ng-app="Calc" ng-controller="Calc_Ctrl"> 
 
       <div data-ng-repeat="choice in choices" class="col-md-12 col-sm-12 col-xs-12 bottom-line no-gap"> 
 
           <h6>Open New Row {{$index + 1}} 
 
            <button type="button" class="btn btn-default pull-right btn-right-gap btn-red" aria-label="Left Align" ng-click="addNewChoice()" style="margin-top: -5px;" id="plus_icon"> 
 
             <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 
 
            </button> 
 

 
           </h6> 
 
           <div class="row walls top-gap"> 
 

 
            <div class="form-group col-md-3 col-sm-3 col-xs-12"> 
 
             <label for="length">Length :</label> 
 
             <input type="number" class="form-control text-red bold" id="length" ng-model="choice.l2" min="0" max="999" maxlength="6" step="0.00"> 
 
            </div> 
 
            <div class="form-group col-md-3 col-sm-3 col-xs-12"> 
 
             <label for="height">Height :</label> 
 
             <input type="number" class="form-control text-red bold" id="height" ng-model="choice.b2" min="0" max="999" maxlength="6" step="0.01"> 
 
            </div> 
 
            
 
            <button type="button" class="btn btn-default pull-right btn-red" aria-label="Left Align" ng-click="removeChoice()" id="minus_icon"> 
 
            </button> 
 
           </div> 
 

 
         </div> 
 
    </div> 
 
</body> 
 

 
</html>

+0

作品这里细[链接](http://plnkr.co/edit/z24mAf6XAeXe714BqWbY?p=preview) –

+0

@SatejS其不工作,其采取整数的n个和字符,多数民众赞成在错误它必须只需要3整数和2浮点值,例如123.33 – bhatt

+0

对不起,在它上面工作。 –

回答

0

所以我已经先行实简化了这段代码。我了解到,您关注的是符合123.33这种格式的价值。

我在输入类型中使用了ng-pattern以确保输入与所需模式相匹配。

正则表达式

​​

的HTML

<input type="number" class="form-control text-red bold" id="length" ng-model="choice.l2" ng-pattern="/[0-9]{3}[.][0-9]{2}/" min="0" max="999" maxlength="6" step="0.00"> 

长度和高度场才有效,如果输入符合要求的正则表达式。

这里是一个demo

+0

抱歉说,但它再次采取n数字,其中ü张贴在纸上 – bhatt

+0

是的,它需要数量为n的数字,但该字段只有符合所需模式时才会生效。这正是Angular的美妙之处,您可以根据模式验证输入。如果它无效,它不会为ng模型赋值。 –

+0

它适合开发者查看,但作为用户查看其无效,因为用户不幸地不知道角的美 – bhatt