2016-06-29 3 views
1

Hallo das Problem ist, dass, wenn Textarea nicht leer ist .. es zeigt, dass alle Felder nicht leer sind, auch wenn Inputs tatsächlich leer sind.überprüfen, ob alle Einträge mit der gleichen Klasse und Textarea wenn beide leer oder nicht

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"> 

und das ist mein Skript Eingaben und Textbereich, wenn leer oder nicht zu bestätigen. Ich habe eine Geige 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') 
    } 

}) gemacht;

+0

u müssen alle Felder allein zu validieren? –

+0

nein ich muss prüfen, ob inputs und das textarea beide leer sind oder nicht..wenn beide leer ich warne alle leeren..wenn ich nicht beide nicht leer..aber das problem ist wenn texarea ist nicht leer und eingaben leer .. es zeigt, dass alles leer ist, –

Antwort

1

diesen Code Versuchen

Sie den Zustand brechen müssen, sobald seine unter

in foreach Zustand kommt

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'); 
     } 

     }); 
    }); 
+0

ty so viel für die Hilfe, es hat funktioniert! –

+0

yea froh, Ihnen zu helfen :) –

1

Das Problem ist, wenn der letzte nicht leer ist, wird der hasNoValue auf false gesetzt, auch wenn einer der Eingänge leer ist.

Sie müssen brechen, wenn. Eine schnelle Lösung wird sein:

$("input.h12lol").each(function() { 
     if(hasNoValue != true){ 
      if (!$(this).val()) { 
       hasNoValue = true; 
      } else { 
       hasNoValue = false; 
      } 
     } 
    }); 
+0

yup, dass es funktioniert ty für thelp –

0

Statt Zyklus mit der jQuery.each Sie können alle Eingabeelemente filtern, um nur die nicht leeren zu erhalten.

$(function() { 
 
    $("#step02").click(function() { 
 
    var hasNoValue = false; 
 
    var inputsNotEmpty = $("input.h12lol").filter(function (index, element) { 
 
     return this.value.trim() != '' 
 
    }); 
 
    if ($("textarea.h12lol").val().trim() == '') { 
 
     hasNoValue = true; 
 
    } 
 
    if ((inputsNotEmpty.length == 0) && (!hasNoValue)) { 
 
     alert('all empty') 
 
    } else { 
 
     alert('all no empty') 
 
    } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<div class="h12"> 
 
    <input class="h12lol" type="text" name="title" placeholder="nom"> 
 
</div> 
 
<br> 
 
<div class="h12"> 
 
    <input class="h12lol" type="text" name="lieu" placeholder="date et lieu de naissance"> 
 
</div> 
 
<br> 
 

 
<div class="h12"> 
 
    <input class="h12lol" type="text" name="adress" placeholder="adresse"> 
 
</div> 
 
<br> 
 
<div class="h12"> 
 
    <input class="h12lol" type="text" name="email" placeholder="Email"> 
 
</div> 
 
<br> 
 
<div class="h12"> 
 
    <input class="h12lol" type="text" name="int" placeholder="Telephone"> 
 
</div> 
 
<br> 
 
<div class="h12"> 
 
    <input class="h12lol" type="text" name="page" placeholder="Les pages"> 
 
</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">

+0

, wenn ich auf Schritt 2 klicke und Text in Textarea lege und die Eingaben leer lasse..es warnt "alles nicht leer" aber es sollte "alles leer" wie das obige Beispiel aussehen –

+0

@MounirFederere Snippet aktualisiert. Gib mir Bescheid – gaetanoM

Verwandte Themen