Mr Guglyak
Mr Guglyak

Reputation: 1

HTML attribute which excludes this value

" <input type="number" min="1" max="100"> *

... I did not know how to exclude 0, so I set the minimum value to 1, Please help)

Upvotes: 0

Views: 57

Answers (3)

Marik Ishtar
Marik Ishtar

Reputation: 3049

As I understood from you question you want to exclude zero, which means all other numbers (< 100) are accepted

try this code: it accepts the negative numbers + positive numbers > 0 and =< 100

const input = document.querySelector("input")
input.addEventListener('change', function(e) {
  if ( e.target.value === "0" ) {
    input.value = 1
  }
})
<input type="number" max="100"/>

Upvotes: 0

Simone Rossaini
Simone Rossaini

Reputation: 8162

You can use jquery like that:

$('input').keypress(function(e){ 
   if (this.value.length == 0 && e.which == 48 ){
      return false;
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" min="1" max="100">

Upvotes: 0

Victor
Victor

Reputation: 478

you cannot exclude via html syntax, you'd need javascript for that. the min value=1 might be the only way via html only.

Upvotes: 1

Related Questions