September 30, 2012

How will I compare a text input if it is empty or not?

Question by eggshot

How will I compare a text input if it is empty or not inside a Jquery statement? Is the code below alright?

$(#'generate').click(function(){
   if(parseInt($('#lastname').val,10)==0){
      alert("Should Not Be Empty!");
   }
}

Answer by Starx

Simply

$('#generate').click(function(){
    if($('#lastname').val().trim() != "") {
       alert("Should Not Be Empty!");
    }
}

Also, there is a typo

  $(#'generate').click(function(){
//   ^   Right here

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!