简体   繁体   English

限制输入字段只接受带范围的数字

[英]Limit the input Field to take only numbers with range

I want to create a input field in html where can limit the users to enter a number only between the range -40 to 130. The user can also enter decimal values For example : -40.2 (valid) -40.23 (not Valid) 130(valid) 130.1 (not Valid) 我想在html中创建一个输入字段,可以限制用户只输入-40至130之间的数字。用户还可以输入十进制值,例如:-40.2(有效)-40.23(无效)130(有效)130.1(无效)

So the input should be able to take in any number between the range and should only accept decimal place fixed to 1. 因此,输入应该能够在范围之间取任意数字,并且只能接受固定为1的小数位。

Any suggestions or help is highly appreciated 任何建议或帮助都受到高度赞赏

thanks in Advance 提前致谢

You can use an input of type number with the attributes min max and step like this : 您可以使用类型为number的输入,其属性为min max和step,如下所示:

<form action="">
    <input type="number" min="-40" max="130" step="0.1" id="input"/>
    <button type="submit">Ok</button>
</form>

I provide a JSFiddle here. 我在这里提供一个JSFiddle。 When you try to submit the form, the html5 validation displays a message if the number is out of the bounds or with more than one decimals. 当您尝试提交表单时,如果数字超出范围或带有多个小数,则html5验证将显示一条消息。

JSFiddle JSFiddle

as Xartok told You can use an input of type number with the attributes min max and step but if the user is keying in the input its a bit hard from my experience. 正如Xartok告诉您的那样,您可以使用具有min max和step属性的number类型输入,但是根据用户的经验,如果用户键入该输入,将有些困难。 what i did was like this. 我所做的就是这样。

  • onkeypress is used to allow users to only key in integers with decimal only. onkeypress用于允许用户仅键入带小数的整数。
  • ng-blur is used to trigger changeDecimal function to do the validation/rounding up to fixed decimal places ng-blur用于触发changeDecimal函数进行验证/舍入到固定的小数位

     <form> <input type="text" id="input" onkeypress="return event.charCode >= 45 && event.charCode <= 57 && event.charCode!=47" ng-model="input1"ng-blur="changeDecimal()" /> <button type="submit">Ok</button> </form> 

and from the controller side what i did was this : 从控制器方面,我所做的就是:

1st i parse the input to float and fix it to 1 decimal place. 首先,我将输入解析为float并将其固定到小数点后一位。 then i made a condition to check the range if it is within the range, the input is replaced with the new value else an alert is returned. 然后我做了一个条件来检查范围是否在范围内,用新值替换输入,否则返回警报。 in the else section i did a small check if the input is blank or not a number then replace with a default value (to avoid a loop of alert if the input is left blank) 在else部分中,我仔细检查了输入是否为空或不是数字,然后替换为默认值(如果输入为空,则避免出现警报循环)

app.controller('MainCtrl', function($scope) {

  $scope.changeDecimal = function (){
    temp = parseFloat($scope.input1).toFixed(1);
    if (temp > -40 && temp < 130 && !isNaN(temp)){
      $scope.input1= temp;
    }else{
      alert("value out of range ");
      if (isNaN (temp) || temp == null || !angular.isDefined(temp)){
        $scope.input1=0;
      }
    }
  }

});

If you plan to use the input type as number what you can do is set a condition for you submit button ( ng-disable ). 如果您打算将输入类型用作数字,则可以设置提交按钮的条件( ng-disable )。 the button is disabled until the condition is met. 该按钮将被禁用,直到满足条件为止。

here is the sample from Plunker 这是Plunker的样品

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

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