Mounir Federere Mounir Federere - 5 months ago 21
HTML Question

check if all inputes with same class and textarea if both empty or not

Hello the problem is that if textarea is not empty..it shows that all fields not empty even if inputes are indeed empty.

Html

<div class="h12">
<input class="h12lol" type="text" name="title" placeholder="nom"></input></div><br>
<div class="h12">
<input class="h12lol" type="text" name="lieu" placeholder="date et lieu de naissance"></input></div><br>
<div class="h12">
<input class="h12lol" type="text" name="adress" placeholder="adresse"></input></div><br>
<div class="h12">
<input class="h12lol" type="text" name="email" placeholder="Email"></input></div><br>
<div class="h12">
<input class="h12lol" type="text" name="int" placeholder="Telephone"></input></div><br>
<div class="h12">
<input class="h12lol" type="text" name="page" placeholder="Les pages"></input></div><br>
<div class="h12">

<input type="hidden" name="MAX_FILE_SIZE" value="2048000" />

<input class="h12lol" style="visibility: hidden;" name="myimage" type='file' multiple accept='image/*' id='logopics' /><br/><br/></div>
<textarea class="h12lol" id='editor1' name="article" style="margin-top: 30px;"></textarea> <input id="step02" class="svx" type="button" name="step" value="step 02">


and this is my script to validate inputs and textarea if empty or not. i have made a fiddle https://jsfiddle.net/2emnrkyg/

Jquery

<script>
$("#step02").click(function() {

var hasNoValue;
var hasNoValue2;
$("input.h12lol").each(function() {

if (!$(this).val()) {


hasNoValue = true;

} else {
hasNoValue = false;
}

});

if ($.trim($("textarea.h12lol").val()).length < 1) {

hasNoValue2 = true;

} else {
hasNoValue2 = false;
}
if ((hasNoValue) && (hasNoValue2)) {
alert('all empty')

} else {
alert('all no empty')
}


});

Answer

Try this code

you need to break the condition once its comes under in foreach condition

Check out the fiddle

  $(document).ready(function(){
        $('#step02').click(function(){
        var inputvalue = 0;
        $("input.h12lol").each(function() {
        if(inputvalue == 0){
        if($(this).val() == ''){        
           inputvalue = 0;
        } else {
       // alert('not empty');
           inputvalue = 1;
        }
        }

        });

        if($("#editor1").val()== ""){
         var textarada = 0;
        } else {
        var textarada = 1;
        }

        if(textarada == 1 || inputvalue == 1){
            alert('all not empty');
        } else {
        alert('all  empty');
        }

        });
    });