How to show blank instead of 0 in a numeric input element

2.9k views Asked by At

HTML numeric input in form is defined as

@Html.TextBoxFor(m => Model.Products[i].Soodkogus,
     new { type = "number", min = 0, @class = "quantity" })

This produces input boxes with content 0.

How to make the input box blank if its value is 0? Only non-zero values should shown.

This is an ASP.NET 6 MVC Razor application. jQuery and Bootstrap 5 are used.

1

There are 1 answers

0
toffler On BEST ANSWER

I don't know what your framework is doing in background when generating this input fields, but if you just want to achieve this logic with jQuery - something like this should work.

$(document).ready(function(){
  $('input').val("");
  $('input').on('input',function(){
    if($(this).val() < 1)
    {
      $(this).val("");
    }
  });
});
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

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

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>