JQuery實現使用者名稱無重新整理驗證的小例子

來源:互聯網
上載者:User

1.在靜態頁面裡添加文字框及樣式和js指令碼的引用:

複製代碼 代碼如下:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://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)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("該使用者名稱可以使用!");

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.