如何使用jquery验证插件在对话框表单上应用验证?

时间:2013-04-18 13:25:50

标签: jquery cordova jquery-mobile jquery-plugins jquery-validate

我试图打开一个单击按钮的对话框,打开的对话框包含一个登录表单,包括用户名和密码以及登录按钮。我的html代码打开一个对话框: HTML代码:  

<body>
<div data-role="page">
 <div data-role="header" data-theme="e">
 <h3>PRAYERS APP</h3>
 </div>
<div data-role="content">
   <div class="center-wrapper">
      <a href="#login_box"  id="showdialog" data-transition="pop" data-rel="dialog" data-role="button" data-icon="plus" data-theme="e">USER LOGIN AREA</a>
   </div>

</div>
 </div>


<div data-role="dialog" id="login_box" class="login-popup" data-dismissible="false">

<a href="#" id="closeBtn" class="ui-btn-right" data-role="button" data-rel="back" data-icon="delete" data-iconpos="notext">close btn</a>  

 <div data-role="content">
    <div class="ui-body ui-body-b">
            <form name="login" id="formlogin" method="get" action="" accept-charset="utf-8">

                <div data-role="fieldcontain">
                    <label for="username">USER NAME:</label><br/>
                    <input type="text" name="username" id="usrname" placeholder="your name"/><br/>
                </div>

                <div data-role="fieldcontain">
                    <label for="password">PASSWORD:</label><br/>
                    <input type="password" name="password" id="usrpswd" placeholder="please enter your password"/><br/>
                </div>

                <fieldset class="ui-grid-a">
                 <div class="ui-block-a">
                 <a href="#" id="signinbtn" data-role="button" data-theme="b" data-icon="check" style="text-align:center; float:right;">SIGN  IN</a></div>
                </fieldset>

                </form>
                    </div>
                      </div>
                       </div>
<!--  -----login page   end------------------->                        
</body>

当我点击“用户登录区域按钮”时,在另一页面上显示我对话框时又有一个问题,而我想在同一页面上打开一个对话框:  JS代码应用FADEIN和FADEOUT:

<head>
<script>
$(document).ready(function()
 {
<!-- ///////////////////////////////login dialog and its validation start//////////////////////////////////////////////////////////--> 

    var loginBox;
          $('#showdialog').click(function()
         {
            loginBox = $(this).attr('href'); 
            //Fade in the Popup and add close button
            $(loginBox).fadeIn(250);
            //Set the center alignment padding + border
            var popMargTop = ($(loginBox).height() + 24) / 2;
            var popMargLeft = ($(loginBox).width() + 24) / 2;

            $(loginBox).css({
                'margin-top': -popMargTop,
                'margin-left': -popMargLeft
                });
            $('body').append('<div id="mask"></div>');
            $('#mask').fadeIn(250);

            return false;
         });

        // When clicking on the close button  or the mask layer the popup closed
         $('#closeBtn, #mask').live('click',function()
          {
             $('#mask, .login-popup').fadeOut(250,function()
                {
                 $('#mask').remove(); 
                });
             return false;
          });
   });
</script>
</head>

现在解决了在打开的对话框的文本框上应用验证的问题,因为我使用此代码但它不起作用:我在脚本标记中应用此代码

$('#login_box').dialog(
    {
    open: function(event,ui)
    {
    $('#formlogin').validate()
    {
    rules:
    {
    username:
        {required: true,
        minlength: 4
        },
        }
    password:
      {required: true,
      minlength: 8
      }
    },
     messages:
     {
     username: 
     {required:"pls enter user name",
     minlength: $.format("Keep typing, at least {0} characters required!"),
     }
    password:
     {
     required:"pls enter password",
     minlength: $.format("password should be atleast {0} characters")
    }

    }
    } 
    }
    });

此外,对话框的Signin按钮无法点击,无法触发此功能:

  $('#signinbtn').on('click',function()
            {
                alert("eerer");
                doLogin();


            });   

我希望这种类型的验证是小提琴:         formvalidation 我已经在我的项目中应用了所有jquery插件:

1 个答案:

答案 0 :(得分:0)

.validate()在表单上初始化插件的方法。将它向上移动一级,以便它立即在DOM就绪处理程序中。

您的.validate()代码中也有很多语法错误。适当的缩进&amp;格式化将帮助您轻松看到它们......

$('#formlogin').validate() { // <-- should be .validate({
    rules: {
        username: {
            required: true,
            minlength: 4
        },
    } // <-- extra } puts password outside of rules
    password: {
        required: true,
        minlength: 8
    }
},
messages: {
    username: {
        required: "pls enter user name",
        minlength: $.format("Keep typing, at least {0} characters required!"), // <-- extra comma
    } // <-- missing a comma
    password: {
        required: "pls enter password",
        minlength: $.format("password should be atleast {0} characters")
    }

}
} // <-- should be a });

这个格式正确......

$(document).ready(function () {

    $('#formlogin').validate({
        rules: {
            username: {
                required: true,
                minlength: 4
            },
            password: {
                required: true,
                minlength: 8
            }
        },
        messages: {
            username: {
                required: "pls enter user name",
                minlength: $.format("Keep typing, at least {0} characters required!")
            },
            password: {
                required: "pls enter password",
                minlength: $.format("password should be atleast {0} characters")
            }
        }
    });

});

简单演示:http://jsfiddle.net/qzdyk/

我为您的submit添加了form按钮以进行演示。该插件会自动捕获并处理click<input type="submit" />的{​​{1}}事件。