• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

how can I set textbox set to 0 if textbox is empty

my textbox is dynamic, upon creating I set the view value to zero and the user can be able to create then upon updating the textbox, I want to set the value into zero if the textbox was empty by user how can I solve this problem using jquery or condition?
For example
if(textbox == empty)
{
     @model = 0
}
else
{
     @model
}
0
jabz ayuap
Asked:
jabz ayuap
  • 3
1 Solution
 
Jerry MillerCommented:
You want to trim the white space in case your users only enter a space, but you can check the length and the n set the value. Something along these lines should work.

var inp = $("#txt").val();
if(jQuery.trim(inp).length <= 0)
{
   $("#txt").val() = 0
}


stackoverflow.com/questions/1565193/check-if-textbox-has-empty-value
0
 
GaryCommented:
Question is slightly confusing what should be happening when.

http://jsfiddle.net/46tm226x/

$(function () {
    $(".mytextbox").change(function () {
        if ($(this).val().trim() == "") {
            $(this).val("0")
        }
    })
})

Open in new window

0
 
jabz ayuapdevAuthor Commented:
my other solution here is
$("#table").keyup(function(){
var press = $(this).val();
if($(this).val < 0)
{
       $(this).val(0.00)
}
0
 
jabz ayuapdevAuthor Commented:
Thank you eenokami, I like to close this question but I don't know how to declare a close one, Sorry for the inconvenience
0
 
jabz ayuapdevAuthor Commented:
Gary answer my question already and I really appreciate his help....
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now