2

I am trying to validate user input in a jquery UI modal dialog. Jquery validation plugin is used for validation.

trying to verify validation in popup button click

<form id="frm">
    <div>
        <button name="btn" id="btn" type="button" >OpneModal</button>
    <div id="dlg" style="display:none">
        <div>
            Name: <input type="text" id="txtName" name="txtName"/>
        </div>
        <div>
        </div>
        </div>
        </div>
    </form>


 $(
     function () {
           var rules = {
            txtName: {
                required: true
            }
        };
        var messages = {
            txtName: {
                required: "Please enter name"
            }
        };
       $("#frm").validate({rules:rules, messages:messages});


          $("#btn").click(
              function () {

                  $("#dlg").dialog(
                      {
                          modal: true,
                          buttons: {
                              "Save": function () {
                                alert(  $("#frm").valid());
                              },
                              "Cancel": function () {
                                  $("#dlg").dialog("close");
              }
                          }

                      }
                      );
              }
              );

      });

But the validation is always success.

code is in js fiddle: http://jsfiddle.net/aGJrZ/6/

2
  • The HTML in your jsFiddle is invalid. It's missing at least two closing div tags: </div>. Commented Aug 24, 2013 at 16:40
  • @Sparky,Corrected and updated the fiddle and entire code added to the question Commented Aug 24, 2013 at 16:46

1 Answer 1

1

The jQuery Validation plugin requires that all input elements must be contained within a set of <form></form> tags. However, using the DOM inspector, you can see that when your dialog box is constructed, it is outside of the <form></form> entirely.

Re-factor the HTML so that the form is contained inside your dialog...

<button name="btn" id="btn" type="button">Open Modal</button>
<div id="dlg" style="display:none">
    <form id="frm">
        <div>
            Name:  <input type="text" id="txtName" name="txtName" />
        </div>
    </form>
</div>

DEMO: http://jsfiddle.net/aGJrZ/9/

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

Comments

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.