jQuery验证插件使用初步

validate是一款优秀的表单验证插件,其初步使用方法如下:

(1)前台页面代码:

<form id="form1" runat="server">
       <div class="divFrame">
         <div class="divTitle">
              请输入下列资料
         </div>
         <div class="divContent">
              <div>
              用户名:<br />
              <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
              *<br />
              <span></span>
              </div>
              <div>
              邮箱:<br />
             <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
              *<br />
              <span></span>
              </div>
         </div>
         <div class="divBtn">
             <asp:Button ID="Button1" runat="server" Text="提交" />
         </div>
     </div>
    </form>

(2)脚本代码:

    <script type="text/javascript" src="Scripts/jquery-1.4.2.js">
    </script>
    <script type="text/javascript" src="Scripts/jquery.validate.js">
    </script>
    <script type="text/javascript" src="Scripts/jquery.validate.messages_cn.js">
    </script>

    <script type="text/javascript">
        $(function () {
            $("#form1").validate(
              {
                  /*自定义验证规则*/
                  rules: {
                      txtUsername: { required: true, minlength: 6 },
                      txtEmail: { required: true, email: true }
                  },
                  /*错误提示位置*/
                  errorPlacement: function (error, element) {
                      error.appendTo(element.siblings("span"));
                  }
              }
            );
        })
    </script>

jquery.validate.messages_cn.js用于将提示信息用中文显示。写验证规则时请注意,用控件的id属性与规则进行关联

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。