刚刚学了jquery的validation数据验证,请问,如何在无刷新的前提下,对用户名是否被占用进行判断。最好有代码。先谢谢了。。在线等

解决方案 »

  1.   

    jquery ajax 接收的用户名到数据库里查一下就行了 具体实现
      

  2.   

    很多方法都可以啊,会AJAX可以用AJAX呵呵,还有一种最笨的方法就是注册旁边放个按钮然,写个查询用户的方法去数据库查询呵呵。<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxProTest.aspx.cs" Inherits="AJAXPro_Pro_AjaxProTest" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript" language="javascript">
            function checkUserName(source)
            {
                var userName = source.value;
                //注意:AJAXPro_Pro_AjaxProTest是AjaxProTest.aspx页面对应的class名称
                //也就是在这里通过JavaScript调用服务器上的方法并接受返回的结果
                var result = AJAXPro_Pro_AjaxProTest.checkName(userName).value;
                document.getElementById("msg").innerHTML = result;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <ul style="margin-left:0px; list-style-type:none;">
            <li>用户名:<input id="txtUserName" type="text" onblur="checkUserName(this)" /></li>
            <li id="msg"></li>
            <li>密码:<input id="txtPassword" type="password" /></li>
        </ul>
        </div>
        </form>
    </body>
    </html>
    [code=C#]
    using System;
    using System.Collections;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;using AjaxPro;public partial class AJAXPro_Pro_AjaxProTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //注册Type
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AJAXPro_Pro_AjaxProTest),this);
        }    /// <summary>
        /// 检查用户名
        /// </summary>
        /// <res>
        /// 有AjaxPro调用的方法必须有AjaxMethod属性
        /// </res>
        /// <param name="userName">要检查的用户名</param>
        /// <returns></returns>
        [AjaxMethod]
        public string checkName(string userName)
            //这个方法是提供前台JavaScript调用的
        {
            bool exists = false;
            if (userName.CompareTo("admin") == 0)
            {
                exists = true;
            }
            if (exists)
            {
                return string.Format("<font color='red'>{0}这个用户已经被注册</font>", userName);
            }
            else
            {
                return string.Format("<font color='green'>{0}这个用户暂未被注册</font>",userName);
            }
        }
    }
    [/code]
      

  3.   

    建议使用 jquery验证中的remote,建立个普通应用程序,使用context.querystring["标签名称"]。应该可以的,不明白再问。