abilash er abilash er - 1 year ago 32
Javascript Question

Value not equal to &nbsp OR value not eqal to space in jQuery

how to detect

space
in jquery ? How to check a value equal to space or
&nbsp
in jquery ?

Please check this code .

Search: <input type="text" name="search-filter" class="search-filter" / >
<input type="submit" value="submit" class="search-img" >

$(".search-img").on("click",function(){
var search_val=$(".search-filter").val();

if(search_val!=="" && search_val!==null ){

alert(search_val);

}
});


Here when i just enter a space and click submit button it is alerting .

Why this code is not working ?

Answer Source

use .trim() to remove white spaces from beginning and end of the string. Try this:

$(".search-img").on("click",function(){
    var search_val=$(".search-filter").val().trim();
     if(search_val!=="" && search_val!==null){
         alert(search_val);
      }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Search:  <input type="text" name="search-filter" class="search-filter" / > 
          <input type="submit" value="submit" class="search-img" >

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download