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.
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.
– Mariana
I edited the question, for clarification.
– Mariana