All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
用ASP.NET开发电子商务网站对数据库表的设计_.net资料_编程技术
创建自定义代码片段 提高CSS布局开发效率_[Html教程]
visual c++中树视图控件(Tree Control)
PHP代码:得到客户端的IP_[PHP教程]
菜鸟的最爱,.NET经典代码汇总(一)_.net资料_编程技术
PHP中使用crypt()实现用户身份验证_[PHP教程]
让SQL Server为工作负载高峰提前做好准备_[SQL Server教程]
C#编程中的,New,关键词的几种用法_.net资料_编程技术
初学来看:动态网页制作技术PHP的变量类型_php资料_编程技术
asp.net各种日期操作大全_[Asp.Net教程]
Reading number is top 10 pictures
Ashlynn Brooke show proud chest measurement1
你是左脑型还是右脑型
关于海盗的研究
影评-疯子,我爱你
奇趣的世界记录1
Startling Russian girl blind date scene2
Distribution of wealth in China survey status report
Seductive beauty of crime1
日本小萝莉1
修电脑也有这么漂亮的美女
Download software ranking
在线棋牌游戏3.05版
SP4 for SQL2000
The Bermuda triangle3
apache-tomcat-6.0.33
Tram sex maniac 2 (H) rar bag8
C语言教程TXT
Unix video tutorial6
Eclipse-CALMSANNY (second edition)
SP3 for SQL2000
天龙八部最新服务端
归海一刀 published in(发表于) 2014/3/16 2:55:28 Edit(编辑)
简单自定义实现jQuery验证_[AJAX教程]

简单自定义实现jQuery验证_[AJAX教程]

简单自定义实现jQuery验证_[AJAX教程]

分两种情况验证,一是直接使用本地验证,二是ajax到服务器验证。


我现在需要验证:用户名,邮箱,电话 三个input(text),用户名、电话号码只需要本地验证格式,只要匹配给定的正则表达式即可,而邮箱首先在本地验证格式,符合格式则ajax到服务器验证是否已被注册,如果被注册了则不能通过验证。


对于每个input后面跟随三种状态,分别表示验证通过、验证未通过、正在提交服务器验证,当未通过验证时,还出示提示信息。


首先设计服务器端的邮箱验证,这里使用.ashx 文件。


<%@ WebHandler Language="C#" Class="validateEmail" %>


using System;
using System.Web;
using System.Data.SqlClient;


public class validateEmail : IHttpHandler {

public void ProcessRequest (HttpContext context) {


if (context.Request.QueryString.Count != 0)
{
string email = context.Request.QueryString[0].Trim();
context.Response.ContentType = "text/plain";
SqlConnection conn = new SqlConnection("Server=localhost;User Id=sa;Password=;Database=XXX");
SqlCommand sqlCmd = new SqlCommand("select count(*) from Clients where email=@email", conn);
sqlCmd.Parameters.AddWithValue("@email", email);


try
{
conn.Open();
int result = (int)sqlCmd.ExecuteScalar();
context.Response.Write("{message:'"+result.ToString()+"'}"); //输出为JSON格式
}
finally
{
conn.Close();
}
}


}

public bool IsReusable {
get {
return false;
}
}


}
接下来实现客户端的html,添加对JQuery的引用


js脚本代码:
//给定需要验证的input添加 needValidate='true' 的属性对,然后选择他们,添加blur的事件函数。
("input[needValidate='true']").blur(function()
{
if(requireField(this))//首先客户端验证
{
if(this.id == 'your_email')//如果是邮件则还进行ajax服务器端验证
{
('#email_img').html("");
.getJSON("validateEmail.ashx",{email:this.value},processValidateEmail);//getJSON获取服务器端的验证结果
}
else
{
('#'+this.id+'_img').html("");
('#'+this.id+'_error').html("");
}
}
}
)



});


//ajax验证完成后的处理函数
function processValidateEmail(data)
{
if(data.message == "0")//表示服务器端没有该邮箱地址
{
('#your_email_img').html("");
('#your_email_error').html("");
}
else
{
('#your_email_img').html("");
('#your_email_error').html("邮箱已被人注册,请更换重试!").attr("style","color:red;");
}
}


//客户端验证函数
function requireField(o)
{
var your_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+/;
var your_name = /^(\w){4,20}|[^u4e00-u9fa5]{2,20}/;
var your_tel = /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+/;
var your_email_error = "请输入正确的邮箱格式!";
var your_name_error = "请输入您的名字,不少于4个字符!";
var your_tel_error = "请输入正确的电话号码格式!";

if(o.value.match(eval(o.id)))
{
return true;
}
else
{
('#'+o.id+'_img').html("");
('#'+o.id+'_error').html(eval(o.id+'_error')).attr("style","color:red;");
}
return false;
}


//submit前的验证函数
function validate() {
var biaozhi = true;
("input[needValidate='true']").each(function(i){
if(!requireField(this))
{ biaozhi = false; }
}
)
return biaozhi;
}
html须验证的表单代码:














  • 以下为javascript正则表达式的参考:

    http://dwww.cn/news/2008-3/20083131746586663.shtml


    http://dwww.cn/news/2007-7/2007761616105130.shtml







    添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


    Disclaimer Privacy Policy About us Site Map

    If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
    Copyright ©2011-
    uuhomepage.com, Inc. All rights reserved.