Check Space in String
<p>I want to check that if my username contains space so then it alert so i do this it work but one problem i am facing is that if i give space in start then it does not alert.I search it but can't find solution, my code is this</p>

<pre><code>var username    =   $.trim($('#r_uname').val());
var space = " ";
  var check = function(string){
   for(i = 0; i &lt; space.length;i++){
     if(string.indexOf(space[i]) &gt; -1){
         return true
      }
   }
   return false;
  }

  if(check(username) == true)
  {
     alert('Username contains illegal characters or Space!');
     return false;
  }
</code></pre>
