jquery exit focus code example

Example 1: onclick focus out jquery

var focus = 0,
  blur = 0;
$( "p" )
  .focusout(function() {
    focus++;
    $( "#focus-count" ).text( "focusout fired: " + focus + "x" );
  })
  .blur(function() {
    blur++;
    $( "#blur-count" ).text( "blur fired: " + blur + "x" );
  });

Example 2: form validation focusin focusout

var name = $('#name').val();
var validName = false;
$('#name').focusin(function(event){
    event.preventDefault();
    if (!validName) {
      $(this).val('');
      $(this).css({ 'border-color': '#fff',
                    'color': 'black'});
    }else if (validName && $(this).val().length >= 4) {
      validName = true;
      name = $('#name').val();
      $(this).val(name);
      $(this).css({ 'border-color': '#fff',
                    'color': '#000'});
    }
  });

  $('#name').focusout(function(){
    if ($(this).val().length <= 3) {
      $(this).val('Minimum 4 characters!');
      $(this).css({ 'border-color': '#cc0000',
                    'color': '#cc0000'});
      validName = false;
    }else if ($(this).val().length >= 4){
      validName = true;
      name = $('#name').val();
      $(this).val(name);
    }
  });