Skip to content Skip to sidebar Skip to footer

Check First Char In String

I have input-box. I'm looking for a way to fire-up alert() if first character of given string is equal to '/'... var scream = $( '#screameria input' ).val(); if ( scream.charAt( 0

Solution 1:

Try this out:

$( '#screameria input' ).keyup(function(){ //when a user types in input boxvar scream = this.value;
    if ( scream.charAt( 0 ) == '/' ) {

      alert( 'Boom!' );

    }
})

Fiddle: http://jsfiddle.net/maniator/FewgY/

Solution 2:

You need to add a keypress (or similar) handler to tell the browser to run your function whenever a key is pressed on that input field:

var input = $('#screameria input');
input.keypress(function() {
  varval = this.value;
  if (val && val.charAt(0) == '/') {
    alert('Boom!');
  }
});

Post a Comment for "Check First Char In String"