之前写过一篇Post用Ajax查询用户名是否重名,这次我们用JQuery的插件validate来实现,更加简单,相比之前用xmlhttprequest来说,不用考虑浏览器兼容的问题,这也是使用是Jquery这个轻量级框架的特点之一。
此处用的是ASP.NET MVC RC,aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestValidation1.aspx.cs" Inherits="DemoMVCForm.Views.FormDemo.TestValidation1" %>
<!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>Demo</title>
<script src="http://www.cnblogs.com/Scripts/jquery-1.2.6.min.js" type="text/javascript"></script>
<script src="http://www.cnblogs.com/Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#form-sign-up").validate({
rules: {
login: {
required: true,
//here invoke related action
remote: '<%=Url.Action("IsLoginAvailable", "FormDemo") %>'
}
},
messages: {
login: {
required: "请输入用户名",
remote: jQuery.format("{0} 已经有人用了")
}
},
// set this class to error-labels to indicate valid fields
success: function(label) {
// set as text for IE
label.html(" ").addClass("checked");
}
});
});
</script>
</head>
<body>
<form action="<%=Url.Action("Register", "FormDemo")%>" method="post" id="form-sign-up">
<h1>Demo表单</h1>
<table id="inputArea">
<tr>
<td>用户名 (试试输入 Petter):</td>
<td><input type="text" name="login" id="login" /></td>
</tr>
<tr>
<td colspan="2" align="center"><br /><input type="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
<!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>Demo</title>
<script src="http://www.cnblogs.com/Scripts/jquery-1.2.6.min.js" type="text/javascript"></script>
<script src="http://www.cnblogs.com/Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#form-sign-up").validate({
rules: {
login: {
required: true,
//here invoke related action
remote: '<%=Url.Action("IsLoginAvailable", "FormDemo") %>'
}
},
messages: {
login: {
required: "请输入用户名",
remote: jQuery.format("{0} 已经有人用了")
}
},
// set this class to error-labels to indicate valid fields
success: function(label) {
// set as text for IE
label.html(" ").addClass("checked");
}
});
});
</script>
</head>
<body>
<form action="<%=Url.Action("Register", "FormDemo")%>" method="post" id="form-sign-up">
<h1>Demo表单</h1>
<table id="inputArea">
<tr>
<td>用户名 (试试输入 Petter):</td>
<td><input type="text" name="login" id="login" /></td>
</tr>
<tr>
<td colspan="2" align="center"><br /><input type="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Asp.net MVC支持Json,所有返回Json的结果,直接使用是JsonResult,看代码cs,很简单:

/// <summary>
/// FormDemoController
/// </summary>
/// <remark>Author : PetterLiu 2009-1-12 17:20 DEV-LIUJUNYUAN</remark>
public class FormDemoController : Controller
{
public JsonResult IsLoginAvailable(string login)
{
//TODO: Do the validation
JsonResult result = new JsonResult();
if (login == "Petter")
result.Data = false;
else
result.Data = true;
return result;
}
}
相关引用:
Asp.net MVC http://www.asp.net/mvc
JQuery http://www.jquery.com
jquery-validation http://bassistance.de/jquery-plugins/jquery-plugin-validation/
Author:PetterLiu http://www.cnblogs.com/wintersun/
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)