Question

http://bassistance.de/jquery-plugins/jquery-plugin-validation/ looks to be the best jquery validation plugin out there. I can't seem to get it working in the jQuery UI dialog though.

This code works outside of the dialog DIV:

<script type="text/javascript">
$(document).ready(function() {
     $("form").validate();
     $("a").bind("click", function() { alert($("form").valid()); });
});
</script>

<form method="get" action="">
   <p>
     Name
     <input id="name" name="name" class="required" minlength="2" />
   </p>
   <p>
     E-Mail
     <input id="cemail" name="email" size="25"  class="required email" />
   </p>
   <a href="#" id="clickTest">Click</a>
</form>

This works great. When i move the form into my dialog div, open dialog, and click the link it returns true, no bueno.

Is there any way to use this killer jquery validation plugin without having to use the <form> tag? Or is there an even better way of doing this successfully?

Was it helpful?

Solution

In case someone else comes across this, the jQuery-UI dialog does not append to the form, it appends just before </body>, so the elements to validate are outside the <form></form> section:

To resolve this, just direct the dialog to move itself inside the form when you create it, like this:

$("#mydiv").dialog("open").parent().appendTo(jQuery("form:first"));

OTHER TIPS

You can use the valitidy jquery plugin

The javascript

function validateForm(){  
    $.validity.start();   
    // Required:  
    $("#recipientFirstName").require();  
    var result = $.validity.end();  
    return result.valid;  
}

$(document).ready(function() { 
    $('#dialog').dialog({
        autoOpen: false,   
        title: 'My title', 
        width: 600,  
        modal: true,  
        buttons: {  
            "Ok": function() {   
                if(validateForm()) {
                    saveOrder();
                    $(".validity-tooltip").css("display", "none"); 
                    $(this).dialog("close");  
                }
            },
            "Cancel": function() {
                // The following line was added to
                // hide the tool-tips programmatically:          
                $(".validity-tooltip").css("display", "none");
                $(this).dialog("close");       
            }
        }
   });
})

Try giving your form an id like "myform".

Then try adding this call to the onclick event of your clicktest anchor :

onclick='return($("#myform").validate().form());'

instead of doing the validation in the document.ready.

Nick Craver solution worked for me, however it doesn't fully work for IE7.

There is a bug in IE7 where it doesn't respect z-index for nested elements; therefore, if you move the dialog's parent into the form, the overlay will be over the dialog, preventing the user from interacting with the dialog. A fix for IE7 is to set the overlay's z-index to -1, then clone the overlay element and add it to the form just like you did for the dialog. Then in the close event of the dialog, remove the cloned overlay. IE7 Z-Index Layering Issues

Depending on your website layout, you might be able to just move the overlay and no need to clone it. I had to clone it, as my website layout has left and right margins, and I needed the overlay to cover the entire area. Below is an example of what I did:

var $dialog = $('#myDialog');
var $form = $dialog.parents('form:first');

$dialog.dialog({
    autoOpen: false,
    title: 'My Dialog',
    minWidth: 450,
    minHeight: 200,
    modal: true,
    position: ['center', 'center'],
    close: function () {
        // IE7 BUG: Needed to add an additional overload because of the z-index bug in IE7.
        $('.ui-widget-overlay-ie7fix:first').remove();
    },
    buttons: dialogButtons
});

$form.prepend($dialog.parent());
$dialog.dialog('open');

if ($.browser.msie && $.browser.version.slice(0, 1) == "7") {
    var $overlay = $('body .ui-widget-overlay:first');
    $form.prepend($overlay.clone().addClass('ui-widget-overlay-ie7fix'));
    $overlay.css('z-index', '-1');
}

Thanks, Garry

This may or may NOT be relevant however this was my problem and solution:

I had the exact same issue, when I opened the dialog my "form" tag got replaced with a "div", tag.

This was because I placed the dialog in an already opened form element, (obviously you can't have a form within a form)

Dont do what I did :)

<form>
    <form>

    </form>
</form>

Have you tried wrapping the form around your div instead (according to the W3C spec form tags are not allowed to be inside divs.)

Without re-writing the plugin I do not see an easy why of doing this without a form element.

I've decided to go with this jQuery validation plugin and write my own plugin around the existing code.

I know this question is old, but I came across this problem too, so I'm posting my solution.

If you want to keep jQuery validation plugin (which seems the best choice) and don't want to move the dialog around like Nick Craver suggested, you can simply add this to the <form> tag:

onsubmit="return false;"

This will prevent the form from ever being submitted. If you need to submit it in some special situations, change this return value when needed.

see this patch to the popular jQuery.validationEngine to allow non-form tags as validation target.

https://github.com/posabsolute/jQuery-Validation-Engine/pull/101

if you find this worth while, leave a comment... as of now the author will not pull the patch.

I have recently created a plugin for HTML Form validation. You can try it yourself.Prashant-UI-Validator

Build using Jquery, supports Bootstrap and you can configure it in your way. It supports different data types like INT, STRING, NUMERIC, MAX[Num], MIN[Num],EMAIL and most importantly your custom javascript validation code as well.

Hope it helps,

I used this an it works in IE8, on an asp.net application using jQuery 1.11.2 and jQueryValidate 1.13.1:

$('#lnz_NuevoLink').click(function () {
        $("#lnz_AgregarDiv").dialog("open").parent().appendTo(jQuery("#aspnetForm"));   //Add the dialog to the form     
        $("#lnz_AgregarDiv").parent().attr('style', 'z-index: 101');                //To avoid the modal
        $("#lnz_AgregarDiv").dialog("option", "position", { my: "center", at: "center", of: window }); //To center the dialog
    })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top