Ich habe einen jQuery-Code, der in meiner Form aussehen sollte, wenn es eine leere Eingabe gibt. Wenn alle Eingaben leer sind, funktioniert mein Code, aber wenn ich einen Wert eingib, funktioniert es nicht mehr und es geht durch. Kannst du mir bitte helfen? Vielen Dank!überprüfen, ob eine Eingabe leer ist (ein Formular validieren)
jQuery(document).ready(function($) {
$('#next').click(function() {//My next button
$('#age, #weight, #target_weight, #size').each(function() {//All field from my form
if ($(this).val().trim().length == '') {//If all fields are empty -> do...
$("html, body").animate({scrollTop: $('.progressbar-header').offset().top-100}, 250);
/*Here I want to show the error field on every empty element*/
$(this).closest('.questions').find('.error').css("visibility","visible");
$(this).css('border-color', "#ff0000");
\t \t \t \t
\t } else {
alert("All fields are filled!");
//This will show the next hidden div and hide the last one
$('.active').removeClass('active').fadeIn().hide()
.next().show().addClass('active');
return false;
}
});
});
});
<div class="field personal-informations-icon">
<div class="questions-fcm-3 styled-radio">
<span class="title">age</span>
<div class="questions">
<input style="border-color: rgb(255, 0, 0);" id="age" class="personal-informations" pattern="[0-9]" name="age" placeholder="in Jahren" value="" min="1" aria-required="true" type="number">
\t <div style="visibility: visible;" class="error age_error">This field is required!</div>
</div>
</div>
<div class="questions-fcm-3 styled-radio">
<span class="title">weight</span>
<div class="questions">
<input style="border-color: rgb(255, 0, 0);" id="weight" class="personal-informations" pattern="[0-9]" name="weight" placeholder="in KG" value="" min="1" aria-required="true" type="number">
\t <div style="visibility: visible;" class="error weight_error">This field is required!</div>
</div>
</div>
<div class="questions-fcm-3 styled-radio">
<span class="title">target weight</span>
<div class="questions">
<input id="target_weight" class="personal-informations" pattern="[0-9]*" name="aim-weight" placeholder="in KG" value="" min="1" aria-required="true" type="number">
\t <div class="error target_weight_error">This field is required!</div>
</div>
</div>
\t \t \t \t \t \t \t \t \t \t \t \t <div class="questions-fcm-3 styled-radio">
<span class="title">Body Size</span>
<div class="questions">
<input id="size" class="personal-informations" pattern="[0-9]*" name="size" placeholder="in cm" value="" min="1" aria-required="true" type="number">
\t <div class="error size_error">This field is required!</div>
</div>
</div>
</div>
OMG danke soooo viel !!! Du hast meinen Tag gerettet! Jetzt verstehe ich die Situation :-) – Johnny97