Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

HTML umeric 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 input box blank if its value is 0 ?
Only non-zero values should shown.

This is ASP.NET 6 MVC Razor application. Jquery and bootstrap 5 are used.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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/bootstrap@5.0.2/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/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading