Focus Textbox function and Check if it is filled

Asked

Viewed 313 times

0

I need a function that when you click on the textbox if it is empty it becomes a color, and if it is filled change the color, but only when you click on it, how can I do it? If you haven’t filled in I’ll call this function:

function (element) {
            $(element).closest('.control-group').removeClass('success').addClass('error');

And if there is, I’ll call this one:

function (element) {
            element.text('OK!').addClass('valid')
                .closest('.control-group').removeClass('error').addClass('success');

Edition: I have this field in the modal, for example:

<div class="input-group">
  <asp:TextBox ID="txtHora" runat="server" class="form-control" onblur="Verifica_Hora(this);"></asp:TextBox>
  <span class="input-group-addon danger"><span class="glyphicon glyphicon-asterisk"></span></span>
</div>

I already have the function that works if I put required:

$(document).ready(function() {
  $('.input-group input[required], .input-group textarea[required], .input-group select[required]').on('keyup change', function() {
    var $form = $(this).closest('form'),
      $group = $(this).closest('.input-group'),
      $addon = $group.find('.input-group-addon'),
      $icon = $addon.find('span'),
      state = false;

    if (!$group.data('validate')) {
      state = $(this).val() ? true : false;
    } else if ($group.data('validate') == "email") {
      state = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($(this).val())
    } else if ($group.data('validate') == 'phone') {
      state = /^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/.test($(this).val())
    } else if ($group.data('validate') == "length") {
      state = $(this).val().length >= $group.data('length') ? true : false;
    } else if ($group.data('validate') == "number") {
      state = !isNaN(parseFloat($(this).val())) && isFinite($(this).val());
    }
    if (state) {
      $addon.removeClass('danger');
      $addon.addClass('success');
      $icon.attr('class', 'glyphicon glyphicon-ok');
    } else {
      $addon.removeClass('success');
      $addon.addClass('danger');
      $icon.attr('class', 'glyphicon glyphicon-asterisk');
    }

  });

  $('.input-group input[required], .input-group textarea[required], 
.input-group select[required]').trigger('change');


});

If it is filled in, it changes the image of the asterisk to ok. And this, changes the textbox from red to normal, if something is typed:

$(document).ready(function() {

  $('#contact-form').validate({
    rules: {
      txtNome: {
        minlength: 2,
        required: true
      }
    },
    highlight: function(element) {
      $(element).closest('.control-group').removeClass('success').addClass('error');
    },
    success: function(element) {
      element.text('OK!').addClass('valid')
        .closest('.control-group').removeClass('error').addClass('success');
    }
  });

});

Only these two only work outside the modal. So in the modal I need to check the fields, when clicked, and when being typed something. I could not make the modal work with required, because if I put required, I can not open the modal with the button click.

2 answers

0

Try this:

function valid(element) {
  $(element)
    .closest('.control-group')
    .removeClass('error')
    .addClass('success');
}

function notValid(element) {
  $(element)
    .closest('.control-group')
    .removeClass('success')
    .addClass('error');
}


$('#textbox').on('focus change', function() {
  if (! $(this).val() || $(this).val().length === 0) {
    return notValid($(this));
  }
  
  valid($(this));
});
.success {
  border: solid 1px green;
}

.error {
  border: solid 1px  red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="control-group">
  <textarea id="textbox">Teste</textarea>
</div>

  • It did not work Luiz Felipe, the fields that I will need to do this check is textbox, I have the function ready pro required, but by being in the modal I can not make open the modal if put the required in the fields inside. I will need to call this function in several textboxs.

  • I edited the question, for clarification.

0

Just capture the event click to deal with when the field is clicked If you also want to remove the effect when leaving, just use the event blur:

$('#i').click(function() {
	if ($(this).val() == '') {
  	   $(this).addClass('vazio');
           $(this).removeClass('preeenchido');
        } else {
  	   $(this).removeClass('vazio');
           $(this).addClass('preeenchido');  
        }
});


$('#i').blur(function() {
  $(this).removeClass('preeenchido');
  $(this).removeClass('vazio');

});
input {
  background-color: #fff;
  width: 200px
}

.vazio {
  background-color: cyan
}

.preeenchido {
  background-color: yellow
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id='i' />

If you also want to do this when the element is with Focus (clicked or moved with tab) you can use the event focus in place of click

  • It did not work Ricardo, I edited the question, for further clarification.

  • Come on, what didn’t work? It even put the event onclick in Textbox only for testing, to see if it displays at least one alert? Something like that: $('#txtHora').click(function() { alert('clicou'); });

  • Remember that a TextBox with runat="server" does not accept an event onclick in javascript, if put OnClick will run a server event, so it is important to associate the click event through the javascript itself, as in the example of the above comment

  • Actually Ricardo I can not take the runat=server, because I use the field for several things, and I would have to change all logic, has any way I can get around this problem ? in case the click will not work in javascript even then.

  • It works if you associate the event in the javascript itself, like this: $('#txtHora').click(function() { .... seu código }); if it were a asp:Buttom, would have the event OnClientClick that it would solve, but the TextBox has not

  • I put the txthora function with click, but it does not return Alert.

  • I tried this way, but the modal does not open, either with onchange or as Blur. $(Function () { $("#txtHora"). live("keyup change", Function() { if ($(this). val() == "") { Alert('required field') } }); });

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.