欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.Mvc 2.0用户客户端验证实例讲解(3)

程序员文章站 2023-12-21 08:06:16
今天给大家讲解下asp.net mvc的客户端验证.通常情况下,我们在页面中对输入的内容多要进行客户端验证,客户端验证一般使用js进行,这里咱们讲解下使用jquery.va...

今天给大家讲解下asp.net mvc的客户端验证.通常情况下,我们在页面中对输入的内容多要进行客户端验证,客户端验证一般使用js进行,这里咱们讲解下使用jquery.validate插件进行客户端验证。
首先咱们看下注册页面的验证效果

Asp.Mvc 2.0用户客户端验证实例讲解(3)

以上验证主要包括
1.用户名不能为空
2.密码不能为空,密码长度不能小于5位数
3.确认密码不能为空,确认密码长度不能小于5位,确认密码必须和密码文本框输入的一致
4.邮箱格式必须正确。

以下是使用jquery.validate插件进行验证的代码

[html]
<%@ page language="c#" inherits="system.web.mvc.viewpage<mvclogin.models.registermodel>" %> 
 
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
 <title>注册页面</title> 
 <script type="text/javascript" src="../../scripts/jquery-1.4.1-vsdoc.js"></script> 
 <script type="text/javascript" src="../../scripts/jquery.validate.js"></script> 
 <script type="text/javascript"> 
 $().ready(function () { 
 $("#form1").validate( 
 { 
 rules: 
 { 
 username: 
 { 
  required: true 
 }, 
 userpwd: 
 { 
  required: true, 
  minlength: 6 
 }, 
 confirpwd: 
 { 
  required: true, 
  minlength: 6, 
  equalto: "#userpwd" 
 
 }, 
 email: 
 { 
  email: true 
 } 
 
 }, 
 messages: 
 { 
 username: 
 { 
  required: "<span style='color:red'>用户名不能为空! </span>" 
 }, 
 
 userpwd: 
 { 
  required: "<span style='color:red'>密码不能为空!</span>", 
  minlength: jquery.format("<span style='color:red'>密码长度不能小于{0}个字符!</span>") 
 }, 
 confirpwd: 
 { 
  required: "<span style='color:red'>确认密码不能为空!<span>", 
  minlength: jquery.format("确认密码长度不能小于{0}个字符!"), 
  equalto: "<span style='color:red'>两次输入密码不一致!</span>" 
 
 }, 
 email: 
 { 
  email: "<span style='color:red'>邮箱输入格式不正确!</span>" 
 } 
 }, 
 onkeyup: false 
 }); 
 
 }); 
 </script> 
</head> 
<body> 
 <div> 
 <br /> 
 
 <p style="font-size:12px;color:red"> 
 
 <%if (viewdata["msg"] != null) 
 {%> 
 <%:viewdata["msg"]%> 
 <%} %> 
 </p> 
 <br /> 
 <%html.beginform("register", "user", formmethod.post, new { name="form1",id="form1"}) ; %> 
 
 
 <table> 
 <tr> 
 <td><%: html.labelfor(m => m.username) %></td> 
 <td> <%: html.textboxfor(m => m.username) %></td> 
 </tr> 
 
 <tr> 
 <td> <%: html.labelfor(m => m.userpwd) %></td> 
 <td> <%: html.passwordfor(m => m.userpwd) %></td> 
 </tr> 
 
 <tr> 
 <td> <%: html.labelfor(m => m.confirpwd) %></td> 
 <td> <%: html.passwordfor(m => m.confirpwd)%></td> 
 </tr> 
 
 <tr> 
 <td> <%: html.labelfor(m => m.email) %></td> 
 <td> <%: html.textboxfor(m => m.email) %></td> 
 </tr> 
 
 <tr> 
 <td> <input type=submit value="提交" /></td> 
 <td></td> 
 </tr> 
 
 
 </table> 
 
 
 
 <%html.endform(); %> 
 
 </div> 
</body> 
</html> 

$("#form1").validate主要包括规则rules和提示信息messages两部分.
例如

rules:
 {
 username:
 {
  required:true
 },
}

表示id为username的文本框输入内容不能为空.

messages:
 {
 username:
 {
  required:"<span style='color:red'>用®?户¡ì名?不?能¨¹为a空?! </span>"
 },

表示id为username的文本框内容如果为空的话,给出提示信息.

以上就是使用jquery.validate插件进行客户端验证的全部过程,希望对大家的学习有所帮助。

上一篇:

下一篇: