无为清净楼资源网 Design By www.qnjia.com

1.在静态页面里添加文本框及样式和js脚本的引用:

复制代码 代码如下:
Code highlighting produced by Actipro CodeHighlighter (freeware)https://www.jb51.net/-->
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>无标题页</title>
    <script  language ="javascript" src ="../jquery-1.3.2.min.js" type ="text/javascript" ></script>
    <script language ="javascript" src ="validator.js" type ="text/javascript" ></script>
    <link type ="text/css" rel="stylesheet" href ="validator.css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        用户名:<input id="txtName" type="text" class ="txtName" />
        <div  id ="result"></div>
    </div>
    </form>
</body>
</html>

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

复制代码 代码如下:
.txtName
{
 border:1px red solid;   
}

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

复制代码 代码如下:

Code highlighting produced by Actipro CodeHighlighter (freeware)https://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)https://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("该用户名可以使用!");

    }
}

标签:
JQuery,无刷新,验证

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com