0

I've login form as shown below,

<a href="#" id="loginLink">click Here to Log in</a>
<div class="formDiv" id="formDiv">
<form name="loginForm" id="loginForm" action="#">
    <label>Email</label><input type="text" name="email" id="email"><br/>
    <label>Passw</label><input type="password" name="pwd" id="pwd"><br/>
    <input type="submit" value="Log in" id="loginbtn" name="loginbtn">
</form>
</div>

I'm showing above form to users using jQuery Dialog,if form without dialog box(i,e normal HTML) then validations are doing without any issue its jsfiddle but if I'm showing form in dialog box then validations are not doing its jsfiddle, may I know the reason behind this ?

1
  • Off-topic: "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance." ~ Where is all of your jQuery? Commented Oct 4, 2013 at 15:50

2 Answers 2

2

TRy http://jsfiddle.net/devmgs/WNMfA/12/

$(document).ready(function(){

$("a#loginLink").on("click",function(e){
  e.preventDefault();

  $(".formDiv").dialog({
            closeOnEscape: false,
            title:"Login Form",
            modal: true,
            resizable: false,
            width:'350px',
            position:'fixed',

            close:function()
            {                    
                $(".ui-dialog-content").dialog("close");
            }
        });  
});

Use $(".formDiv") dont recreate it inside open function.

Sign up to request clarification or add additional context in comments.

Comments

1

You have several major flaws...

1) Your code that calls .validate()...

$(document).on("click", '#loginbtn', function(e){
    e.preventDefault();
    $("#loginForm").validate();        
    alert('test');  
    //I want to do Ajax stuff   
});

Do not call .validate() within a click handler because it only needs to be called one time. The .validate() method is not the method for calling validation on the form. It is only the method for initializing the plugin on the form. So just like in your working example, you must call .validate() as soon as the form is constructed, which is normally within the DOM ready event. The entire on('click') handler function above can be removed.

2) Your code that opens the dialog...

open:function(){                   
    $(this).html($("#formDiv").html());
},

Within your open callback, you duplicate the HTML from a hidden div into your dialog div for the form. The main problem with this technique is that you now have more than one element on the same page with with same id. Not only is this invalid HTML, but the jQuery Validate plugin will only apply to the first instance, the original hidden instance, of this id. Remove the open callback and attach the hidden div to .dialog() like this: $("#formDiv").dialog( ...

3) Your comment indicates you want to submit this form with ajax(). If that's the case, use the submitHandler callback function of the jQuery Validate plugin. As per documentation, this is the "right place to submit a form via Ajax after it is validated."

$("#loginForm").validate({ // initialize the plugin
    submitHandler: function (form) {
        // your ajax here;               // submit the data
        $("#formDiv").dialog("close");   // close the dialog
        alert('submitted with ajax');    // for demo 
        return false;                    // prevent regular form submit action
    }
});

Working DEMO: http://jsfiddle.net/TRHxZ/

$(document).ready(function () {

    $("#loginForm").validate({ // initialize the plugin
        submitHandler: function (form) {
            // your ajax here;               // submit the data
            $("#formDiv").dialog("close");   // close the dialog
            return false;                    // prevent regular form submit action
        }
    });

    $("a#loginLink").on("click", function (e) {
        e.preventDefault();

        $("#formDiv").dialog({
            closeOnEscape: false,
            title: "Login Form",
            modal: true,
            resizable: false,
            width: '350px',
            position: 'fixed',
            close: function () {
                $(".ui-dialog-content").dialog("close");
            }
        });
    });

});

2 Comments

You have said my question is offtopic,after 1 hour you have given a lengthy answer though the previous answers described the same briefly,anyhow thanks for your effort in answering my offtopic question.
@Mahesh.D, it's simply off-topic for not showing full code in the question. Copy your jQuery code from your jsFiddle into your OP and it will be on-topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.