13

I have a text field.

<input type="number" min="0" max="999">

But i am able to type as many numbers inside the text box.

As I set max=999, I don't want that to happen. Anyone knows the solution for this? I have tried ng-maxlength and maxlength but doesn't work.

Shrabanee
  • 2,706
  • 1
  • 18
  • 30
vishnuprasad kv
  • 990
  • 5
  • 22
  • 46

7 Answers7

52

max attribue specifies the maximum possible value that we can specify.

for more details see this link

I think the following will be helpful to you,

//maxlength="10"
<input type="number" onKeyPress="if(this.value.length==10) return false;" />
Community
  • 1
  • 1
3

The solution that worked for me is to use onkeypress with the max attribute. For e.g. for max length of 999

<input type="number" max="999" onkeypress="if (this.value.length > 2) return false;"/>

Tested in Chrome, Firefox and Edge.

Scorpionk
  • 418
  • 6
  • 13
2

Try This

var jimApp = angular.module("mainApp",  []);

jimApp.controller('mainCtrl', function($scope){
  var oldNumber = 0;
  $scope.numberChanged = function(number){
    if(number<=999){
      oldNumber = number;
    }
    return oldNumber;
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<div  ng-app="mainApp" ng-controller="mainCtrl">
  <input type="number" min="0" max="999" ng-model="jimNum" ng-change="jimNum = numberChanged(jimNum);">
</div>
byteC0de
  • 5,153
  • 5
  • 33
  • 66
1

Try

<input type="number" min="0" max="999" 
   onKeyUp="if(this.value>999){this.value='999';}else if(this.value<0){this.value='0';}"
id="yourid">
mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • Note that if the user adjusts the value using the field controls, they likely will not dispatch any keyboard event, so the *onkeyup* listener won't be called. – RobG May 31 '16 at 07:13
0

There is JSFiddle, using JQuery.

Your input

<input type="number" id="num">

The script :

$("#num").keypress( function(e) {
    var current_val = $(this).val();
    var typing_char = String.fromCharCode(e.which);
if (parseFloat(current_val + "" +typing_char) > 900) {
    return false;
}

})
Jax Teller
  • 1,447
  • 2
  • 15
  • 24
  • That restricts the maximum value to 900, and allows any size negative number. Far better would be something like `var v = this.value; if (v > 999) this.value = v.substr(0,2)` with additional logic for negative numbers (whatever they might be). – RobG May 31 '16 at 07:07
  • yeah, my bad, forgot about the min val, but he can fix it easily with all answer now. – Jax Teller May 31 '16 at 07:12
0

if you are limit the single field

  <TextField type="number"
    className="text-field-amount"
    onInput={(e)=>{ 
        e.target.value = Math.max(0, parseInt(e.target.value) ).toString().slice(0,2)
    }}
    min={0}
0

if you are mapping more then one number Text field through map then do like this

     <TextField
                      key={key}
                      label={row.q_desc}
                      type="number"
                      onChange={this.onChange}
                      onInput={this.onInput}

                    />

    onInput= event => {
    event.preventDefault();

    debugger;
    var textField_name = this.state.input_Name;
    if (textField_name == "Zip") {
      event.target.value = Math.max(0, parseInt(event.target.value))
        .toString()
        .slice(0, 4);
    }

    if (textField_name == "Social Security") {
      event.target.value = Math.max(0, parseInt(event.target.value))
        .toString()
        .slice(0, 9);
    }
    if (textField_name == "Phone") {
      event.target.value = Math.max(0, parseInt(event.target.value))
        .toString()
        .slice(0, 10);
    }
  };



 onChange(e) {
          debugger;

         this.setState({
            input_Name: e.target.name
         });
  }
Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39