<!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>
<title>后台验证方式</title>
<script src="jqValidate/jquery.min.js" type="text/javascript"></script>
<script src="jqValidate/jquery.metadata.js" type="text/javascript"></script>
<script src="jqValidate/jquery.validate.js" type="text/javascript"></script>
<script src="jqValidate/jquery.validate.messages_cn.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#signupForm").validate({
onkeyup: true,
rules: {
answer:{
required: true,
remote: {
url: "Handler.ashx", //后台处理程序
type: "post", //数据发送方式
dataType: "json", //接受数据格式
data: { //要传递的数据
answer: function() {
return $("#answer").val();
}
}
}
}
},
messages: {
answer: {
required: "请输入姓名",
remote: "远程请求不正确"
}
}
});
});
</script>
</head>
<body>
<form id="signupForm" method="get" action="">
<p>
<label for="firstname">
请输入答案: </label>
<input id="answer" name="answer" />
</p>
<p>
<input class="submit" type="submit" value="Submit" />
</p>
</form>
</body>
</html>
2. Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string result = context.Request["answer"] == "OK" ? "true" : "false";
context.Response.Write( result );
}
public bool IsReusable {
get {
return false;
}
}
}