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

JQuery实现用户名无刷新验证的小例子_jquery

程序员文章站 2022-05-26 13:49:43
...
1.在静态页面里添加文本框及样式和js脚本的引用:

复制代码 代码如下:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.jb51.net/-->


无标题页







用户名:





2.css样式表,当文本框文字为空时边框红色:

复制代码 代码如下:

.txtName
{
border:1px red solid;
}

3.js脚本:当文本框文字为空时边框红色;如果用户名为janes,则提示“用户名已经存在”,否则提示“用户名可以使用”。

复制代码 代码如下:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.jb51.net/-->$(function(){
var txtname=$("#txtName");
//输入文字时文本框样式
txtname.keyup(function(){
var name=$(this).val();
if(name=="")
$(this).addClass("txtName");
else $(this).removeClass("txtName");
})
//失去焦点时验证用户名是否可用
$("#txtName").blur(function()
{
var name=$(this).val();
$.get("validator1.aspx?name="+name,null,function(response){
$("#result").html(response);
})

})
})

4..aspx及.cs页面代码,用来验证用户名是否可用,以返回结果。

复制代码 代码如下:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.jb51.net/-->public partial class Validator_validator1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string name = Request.QueryString["name"].ToString();
if (name == "janes")
Response.Write("该用户名已经存在!");
else
Response.Write("该用户名可以使用!");

}
}