[jQuery] Form Plugin + Validator

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • [jQuery] Form Plugin + Validator

    Hi Leute,
    ich benutze dieses Form Plugin für jQuery und will es nun in Verbindung mit dem Validation Plugin benutzen. Folgendes Problem tritt dabei auf: Wenn ich das Formular abschicke obwohl einige Eingaben nicht korrekt waren (was auch vom Validation Plugin korrekt angezeigt wird), wird das Formular trotzdem abgeschickt...

    Wie kann ich das vermeiden?

    Ich habe momentan folgenden Quelltext (Die Kommentare von den Demo-Seiten lass ich mal drinnen..)

    Quellcode

    1. $(document).ready(function(){
    2. check = $("#commentForm").validate({
    3. rules: {
    4. datei: {
    5. accept: "jpg|gif"
    6. }
    7. }
    8. });
    9. var options = {
    10. target: '#output2', // target element(s) to be updated with server response
    11. beforeSubmit: showRequest, // pre-submit callback
    12. success: showResponse // post-submit callback
    13. };
    14. // bind to the form's submit event
    15. $('#commentForm').submit(function() {
    16. // inside event callbacks 'this' is the DOM element so we first
    17. // wrap it in a jQuery object and then invoke ajaxSubmit
    18. $(this).ajaxSubmit(options);
    19. // !!! Important !!!
    20. // always return false to prevent standard browser submit and page navigation
    21. return false;
    22. });
    23. });
    24. // pre-submit callback
    25. function showRequest(formData, jqForm, options) {
    26. // formData is an array; here we use $.param to convert it to a string to display it
    27. // but the form plugin does this for you automatically when it submits the data
    28. var queryString = $.param(formData);
    29. // jqForm is a jQuery object encapsulating the form element. To access the
    30. // DOM element for the form do this:
    31. // var formElement = jqForm[0];
    32. alert('About to submit: \n\n' + queryString);
    33. // here we could return false to prevent the form from being submitted;
    34. // returning anything other than false will allow the form submit to continue
    35. return true;
    36. }
    37. // post-submit callback
    38. function showResponse(responseText, statusText) {
    39. // for normal html responses, the first argument to the success callback
    40. // is the XMLHttpRequest object's responseText property
    41. // if the ajaxSubmit method was passed an Options Object with the dataType
    42. // property set to 'xml' then the first argument to the success callback
    43. // is the XMLHttpRequest object's responseXML property
    44. // if the ajaxSubmit method was passed an Options Object with the dataType
    45. // property set to 'json' then the first argument to the success callback
    46. // is the json data object returned by the server
    47. alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
    48. '\n\nThe output div should have already been updated with the responseText.');
    49. }
    Alles anzeigen
  • Hi, ich hab mich nicht in die Materie eingearbeitet, aber du definierst "nur" Regeln, aber nicht den SubmitHandler.

    Siehe Beispiel "Integration with Form Plugin (AJAX submit)" [1]

    Quellcode

    1. var v = jQuery("#form").validate({
    2. submitHandler: function(form) {
    3. jQuery(form).ajaxSubmit({
    4. target: "#result"
    5. });
    6. }
    7. });


    [1] jquery.bassistance.de/validate…it-intergration-demo.html
  • Ok, das funktioniert jetzt alles ganz gut soweit..
    Nur ich habe das Problem, dass mein JavaScript Code nicht greift, wenn ich einfach im letzten Eingabefeld die Eingabetaste drücke.. Wenn ich mit der Maus auf den Submit Button drücke klappt es komischerweise so wie es klappen sollte..
    Wenn ich die Enter-Taste drücke wird mein JavaScript anscheinend schlichtweg ignoriert :( :( Woran liegt das?

    Hier mein Code:

    Quellcode

    1. <script type="text/javascript">
    2. jQuery(function() {
    3. var v = jQuery("#form").validate({
    4. rules: { datei: {accept: "jpg|gif"}},
    5. submitHandler: function(form) {
    6. check = confirm("Sind Sie sicher, dass diese Eingaben richtig sind?")
    7. if (check == true) {
    8. jQuery(form).ajaxSubmit({
    9. target: "#result",
    10. beforeSubmit: showRequest,
    11. success: toggle(form)
    12. });
    13. return false;
    14. }
    15. }
    16. });
    17. });
    18. function toggle(ref) {
    19. $(ref).slideToggle("slow");
    20. }
    21. </script>
    Alles anzeigen


    Quellcode

    1. <form enctype='multipart/form-data' action='publish.php?do=add' method='post' class='cmxform' id='form'>
    2. <p><b>*</b>Titel: <br><input tpye='text' name='titel' class='required' minlength='8'></p>
    3. <p><b>*</b>Beschreibung: </p><textarea name='beschreibung' cols='45' rows='7' class='required' minlength='8'></textarea>
    4. <p><b>*</b>Vorschaubild: <br><input type='file' name='datei' class='required'>
    5. <span class='formInfo'><a href='./help/help.html?width=375' class='jTip' id='one' name='Hilfe:'>?</a></span></p>
    6. <input type='submit' class='submit' name='submitButton' value='Weiter'>
    7. <br><br><b><p>Die mit * gekennzeichneten Felder müssen ausgefüllt werden!</p></b>
    8. </form>


    Ich hoffe mal mir kann da wieder jemand helfen =)

    Gruß

    Ollo