Home > Web Front-end > JS Tutorial > body text

JQuery ajax实现用户名无刷新验证

WBOY
Release: 2016-06-01 09:54:22
Original
1114 people have browsed it

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

<code class="language-html">

    <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">


    <form id="form1" runat="server">
    <div>
        用户名:<input id="txtName" type="text" class="txtName">
        <div id="result"></div>
    </div>
    </form>

</code>
Copy after login

 

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

<code class="language-css">.txtName
{
 border:1px red solid;    
}</code>
Copy after login

 

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

<code class="language-javascript">$(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);
  })
})
})</code>
Copy after login

 

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

<code class="language-cs">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("该用户名可以使用!");

    }
}</code>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!