2016-10-27 5 views
-1

Ich benutze form validation jquery. Und Eingang bilden wie diesecheck form validation auf jquery

<form id="form-valid"> 
<select id="country" name="country" class="input-group form-control input-sm"> 
    <option value="Asia">Asia</option> 
    <option value="America">America</option> 
    <option value="Eropa">Eropa</option> 
    <option value="Africa">Africa</option> 
    <option value="Australia">Australia</option> 
</select> 
<input type="text" id="codetax" name="codetax"><input> 
<select id="status" name="status" class="input-group form-control input-sm"> 
    <option value="small">samll island</option> 
    <option value="big">big island</option> 
    <option value="medium">medium island</option> 
</select> 
<button class="btn btn-default btn-primary btn-sm btn-form-filter form-control" data-target="tmppo" id="btnsave" type="button">Save</button> 
</form> 

Aber mein Problem ist, ich brauche zu überprüfen, ob Land Asien, taxcode und Status muss gefüllt. Aber wenn Land nicht Asien, Steuercode und Status kann leer sein. Wie mache ich das, wenn ich den Submit Button verwende? Ich bin noch neu über JQuery Formular validieren. Bitte hilf mir.

aktualisieren ok es funktioniert jetzt ist, ich tue dies auf meinem von Validate mit hängt

$('#form').validate({ 
      errorElement: 'span', 
      errorClass: 'help-block', 
      rules: { 
       country: { 
        required: true 
       }, 
       taxcode: { 
        required: { 
         depends: function(element) { 
          if ($('#country').val() == 'Asia') { 
           return true; 
          } else { 
           return false; 
          } 
         } 
        } 
       }, 
      }, 
      messages: { 
       codetax : "Pleasa instert taxcode 
      }, 
     }); 

Und auf HTML-Code habe ich „name“ verwenden, um Regeln zu bekommen. Für alle TQ mir geholfen :)

+0

Was haben Sie bisher versucht? Sende deinen Code! Was ist passiert, als du es ausgeführt hast? Was haben Sie stattdessen erwartet? Mit was genau hast du Probleme? – Robert

Antwort

1

Ich glaube, Sie die integrierte Methode der über .addMethod()validate Bibliothek geliefert verwenden möchten, finden auf die documentation site. Ich habe deine Form geändert, um sie besser anzupassen.

<script> 
$(document).ready(function() { 
    // Add the method here, you can modify the code inside as need be 
    $.validator.addMethod("country", function(value, element) { 
     if(value == 'Asia') { 
      var getCodeTax = $('input[name=codetax]').val(); 
      if(getCodeTax === '') { 
       return false; 
      } 
     } 

     return true; 

    }, 'You must select tax code'); 

    $('form').validate({ 
     rules: { 
      status:"required", 
      // This applies the country method above to the country dropdown 
      country: "country" 
     }, 
     messages: { 
     } 
    }); 
}); 
</script> 
<form id="form-valid" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
<select name="country" id="country" class="input-group form-control input-sm"> 
    <option value="">Select</option> 
    <option value="Asia">Asia</option> 
    <option value="America">America</option> 
    <option value="Eropa">Eropa</option> 
    <option value="Africa">Africa</option> 
    <option value="Australia">Australia</option> 
</select> 
<input type="text" id="codetax" name="codetax" /> 
<select id="status" name="status" class="input-group form-control input-sm"> 
    <option value="small">samll island</option> 
    <option value="big">big island</option> 
    <option value="medium">medium island</option> 
</select> 
<input class="btn btn-default btn-primary btn-sm btn-form-filter form-control" value="save" data-target="tmppo" id="btnsave" type="submit"> 
</form> 
+0

kann ich "if" auf $ ('form'). Validate ({rules: ??? – Wolfzmus

+0

Ich glaube, Sie können eine Funktion 'rules: {country: function() {wenn ...' aber ich denke die bevorzugte Methode nach der Dokumentation ist das Hinzufügen einer Methode – Rasclatt

+0

okay, ich werde es versuchen .. Tq :) – Wolfzmus

0

Sie erstellen eine Funktion zu prüfen, welche Art von Land auf die Schaltfläche Click-Ereignis ausgewählt

$("btnsave").click(function() { 
    var country = $("country").val(); 

    if (country == 'Asia') 
    { 
    //show required message here. 
    } 
} 
+0

danke für die Korrektur mich @guradio. Mabuhay kababayan.! lols .. – bogzy

0

ändern Typ der Schaltfläche einreichen.

$(document).on('submit', '#form-valid' function(e){ 
var country = $("#country").val(); 
if(country == 'Asia'){ 
    if($('#codetax').val()=="" || $('#status').val()==""){ 
     e.preventDefault(); 
     //now form won't submit. Still need to add error messages. 
     } 
    } 
}); 

Wenn Sie nicht möchten, dass die Art der Schaltfläche ändern dann einreichen:

$(document).on('click', '#btnsave' function(e){ 
var country = $("#country").val(); 
if(country == 'Asia'){ 
    if($('#codetax').val()!="" && $('#status').val()!=""){ 
     $('#form-valid').submit(); 
    } 
    else{ 
     //add your error messages 
    } 
} 
}); 
+0

Nun, scheint Arbeit für mich, aber meine Frage ist, wie man auf $ ('# Form') verwenden. – Wolfzmus