ASP.NET jQuery 实例12 通过使用jQuery validation插件简单实现用户注册页面验证功能

 更新时间:2012年02月03日 17:08:19   作者:  
在这节我们将使用validation插件实现注册页面的验证功能,通过这个例子,可以更全面的掌握该插件的使用功能
页面样式代码:
复制代码 代码如下:

<style type="text/css">
.header
{
background-color: #CCCCCC;
color: White;
font-size: x-large;
}
.content
{
background-color: White;
font-weight: lighter;
font-size: small;
}
.content td
{
text-align: left;
}
.mandatory
{
color: Red;
}
.errorContainer
{
display: none;
margin-bottom: 20px;
}
.alertMsg
{
margin-left: 20px;
color: #660000;
}
.input-highlight
{
background-color: #CCCCCC;
}
</style>

页面结构代码:
复制代码 代码如下:

<form id="form1" runat="server">
<div align="center">
<div align="center" class="errorContainer">
<fieldset style="width: 550px;">
<p align="left" class="alertMsg">
这里有一些错误注册提示信息,根据提示请核实您注册信息格式是否正确:
</p>
</fieldset>
</div>
<fieldset style="width: 350px; height: 400px">
<table cellpadding="3" cellspacing="3" border="0" class="content">
<tr>
<td colspan="2" class="header" style="text-align: center;">
注册用户
</td>
</tr>
<tr>
<td>
用户名<span class="mandatory">*</span>
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email<span class="mandatory">*</span>
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
密码<span class="mandatory">*</span>
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
确认密码<span class="mandatory">*</span>
</td>
<td>
<asp:TextBox ID="txtConfirmPwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
生日<span class="mandatory">*</span>
</td>
<td>
<asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
地址1<span class="mandatory">*</span>
</td>
<td>
<asp:TextBox ID="txtAddress1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
地址2
</td>
<td>
<asp:TextBox ID="txtAddress2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
邮政编号<span class="mandatory">*</span>
</td>
<td>
<asp:TextBox ID="txtPostal" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
网站
</td>
<td>
<asp:TextBox ID="txtWebsite" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:CheckBox ID="chkTandC" runat="server" />我接受相关法律条款
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<asp:Button ID="btnSubmit" Text="提交" runat="server" />&nbsp;<asp:Button ID="btnReset"
Text="重置" runat="server" />
</td>
</tr>
</table>
</fieldset>
</div>
</form>

脚本代码:

先引入脚本文件:
复制代码 代码如下:

<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="Scripts/jquery.form.js" type="text/javascript"></script>

脚本功能代码:
复制代码 代码如下:

<script type="text/javascript">
$(document).ready(function () {
// 在这里设置默认的操作行为
$.validator.setDefaults({
highlight: function (input) {
$(input).addClass("input-highlight");
},
unhighlight: function (input) {
$(input).removeClass("input-highlight");
}
});

// 然后调用函数validate()开始对form校验
$("#form1").validate({
rules: { txtName: "required",
// 验证邮件格式是否正确,设置email属性为true
txtEmail: { required: true, email: true },
txtPassword: { required: true, minlength: 8 },
// 在这里equalTo属性的作用是验证确认密码必须和之前输入的密码相等
txtConfirmPwd: { required: true, minlength: 8, equalTo: "#txtPassword" },
// 验证日期格式是否正确,设置date属性为true;
txtDOB: { required: true, date: true },
// 通过属性maxlength设置文本最大长度
txtAddress1: { required: true, maxlength: 200 },
txtAddress2: { maxlength: 200 },
// digits属性设置输入的内容必须为数字
txtPostal: { required: true, digits: true },
// url属性验证是否输入为合法的网址
txtWebsite: { url: true },
chkTandC: "required"
},
messages: { txtName: "请输入您的姓名",
txtEmail: { required: "请输入您的Email",
email: "请输入一个合法的email地址"
},
txtPassword: { required: "请输入您的密码",
minlength: "密码长度必须大于8"
},
txtConfirmPwd: { required: "请输入您的确认密码",
minlength: "确认的密码长度必须大于8",
equalTo: "请保证输入的密码和确认的密码要一样"
},
txtDOB: { required: "请输入您的生日",
date: "您输入的生日日期格式不对"
},
txtAddress1: { required: "请输入您的地址1",
maxlength: "您输入的地址1长度不能超过200"
},
txtAddress2: { maxlength: "您输入的地址1长度不能超过200" },
txtPostal: { required: "请输入您的邮政编号",
digits: "您输入的邮政编号必须都为数字"
},
txtWebsite: { url: "请输入一个合法的网址" },
chkTandC: { required: "请接受相关法律条款" }
},
wrapper: "li",
errorContainer: $("div.errorContainer"),
errorLabelContainer: $("#form1 p.alertMsg")
});

$("#btnReset").click(function (e) {
e.preventDefault();
// 这里使用了插件form的resetForm()函数,恢复到第一次加载页面的状态
$("#form1").resetForm();
});
});
</script>

注意:样式选择器div.errorContainer和div .errorContainer的含义不一样,第一个是选择所有带样式类errorContainer的div元素,而第二个是选择div元素里面的后代所有带样式类errorContainer的元素。
最终效果图:

相关文章

  • jQuery中:focus选择器用法实例

    jQuery中:focus选择器用法实例

    这篇文章主要介绍了jQuery中:focus选择器用法,以实例形式分析了:focus选择器的功能、定义及匹配获取焦点元素的使用技巧,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12
  • 使用jQuery处理AJAX请求的基础学习教程

    使用jQuery处理AJAX请求的基础学习教程

    这篇文章主要介绍了使用jQuery处理AJAX请求的基础学习教程,除此之外还引申了链式处理事件回调的写法,由浅入深非常值得借鉴,需要的朋友可以参考下
    2016-05-05
  • 终于实现了!精彩的jquery弹幕效果

    终于实现了!精彩的jquery弹幕效果

    终于实现精彩的jquery弹幕效果了,这篇文章就为大家详细介绍了jquery弹幕效果的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • 如何使用jquery动态加载js,css文件实现代码

    如何使用jquery动态加载js,css文件实现代码

    在jquery中要实现动态加载js文件的方法有很多种,最简单的我们可以直接利用$.include()方法来实现,感兴趣的朋友可以参考下哈
    2013-04-04
  • 基于jQuery实现选取月份插件附源码下载

    基于jQuery实现选取月份插件附源码下载

    这是一个基于jQuery的可以选择年份和月份的月份拾取插件,你可以设置点击页面上的任意元素触发弹出年月选择面板,可以是一个链接也可以是一个输入框,广泛应用于月份查询,而无需设置select表单。
    2015-12-12
  • 使用JQuery制作简易留言板

    使用JQuery制作简易留言板

    这篇文章主要为大家详细介绍了如何使用JavaScript和JQuery制作简易留言板功能,文中的示例代码讲解详细,有需要的小伙伴可以参考一下
    2016-05-05
  • PHP+jQuery实现随意拖动层并即时保存拖动位置

    PHP+jQuery实现随意拖动层并即时保存拖动位置

    本文讲解了如何采用PHP+MySQL+jQuery,实现随意拖动层并即时保存拖动位置。 十分的实用,有需要的小伙伴可以参考下。
    2015-04-04
  • jQuery实现带3D切割效果的轮播图功能示例【附源码下载】

    jQuery实现带3D切割效果的轮播图功能示例【附源码下载】

    这篇文章主要介绍了jQuery实现带3D切割效果的轮播图功能,结合实例形式分析了jQuery轮播图相关的界面布局、3D效果实现与事件响应等相关操作技巧,并附带源码供读者下载参考,需要的朋友可以参考下
    2019-04-04
  • 使用jQuery.Pin垂直滚动时固定导航

    使用jQuery.Pin垂直滚动时固定导航

    这篇文章主要为大家详细介绍了使用jQuery.Pin垂直滚动时固定导航的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • jQuery时间戳和日期相互转换操作示例

    jQuery时间戳和日期相互转换操作示例

    这篇文章主要介绍了jQuery时间戳和日期相互转换操作,结合实例形式分析了jQuery针对日期与时间戳的转换、运算相关操作技巧,需要的朋友可以参考下
    2018-12-12

最新评论