使用asp.net即时用户名检查数据库的可用性

时间:2011-09-21 09:36:25

标签: asp.net ajax json

我正在尝试使用以下网站中检查用户名可用性的最佳示例之一 http://www.highoncoding.com/Articles/439_Performing_Instant_UserName_Availability_Check_Using_JQuery_Ajax_API.aspx

而且只是通过一些预先启动的名称验证,但我不想尝试使用我的数据库,任何人都可以建议我如何继续进行。

以下是代码:

    <script language="javascript" type="text/javascript">

var userName = '';

$(document).ready(function() 
{
    $("#txtUserName").blur(function()
    {
        userName = $(this).val(); 

        if(userName.length <= 6) 
        {
            $("#display").text("username must be atleast 7 characters"); 
            $("#display").css("background-color","red");
        }

        else 
        {
            $.ajax(
            {
                type:"POST",
                url:"AjaxService.asmx/CheckUserNameAvailability",
                data:"{\"userName\":\"" + userName + "\"}",
                dataType:"json",
                contentType:"application/json",
                success: function(response) 
                {
                    if(response.d == true) 
                    {
                        $("#display").text("username is available");
                         $("#display").css("background-color","lightgreen");
                    }
                    else
                    {
                      $("#display").text("username is already taken");
                         $("#display").css("background-color","red");
                    }
                }
            });
        }


    });
});


</script>

这个AjaxService.asmx:

Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml.Linq
Imports System.Collections.Generic

Namespace JQueryUserNameAvailability


    <WebService([Namespace] := "http://tempuri.org/")> _
    <WebServiceBinding(ConformsTo := WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    <System.Web.Script.Services.ScriptService> _
    Public Class AjaxService
        Inherits System.Web.Services.WebService
        <WebMethod> _
        Public Function CheckUserNameAvailability(userName As String) As Boolean
            Dim userNames As List(Of [String]) = New List(Of String)() From { _
                "azamsharp", _
                "johndoe", _
                "marykate", _
                "alexlowe", _
                "scottgu" _
            }

            Dim user = (From u In userNames Where u.ToLower().Equals(userName.ToLower())u).SingleOrDefault(Of [String])()

            Return If([String].IsNullOrEmpty(user), True, False)
        End Function

    End Class
End Namespace

修改后的代码:

 Public Function CheckUserNameAvailability(ByVal userName As String) As Boolean
    Dim strSql As String = String.Format("SELECT COUNT(UserNameCol) FROM Registration WHERE UserNameCol = '{0}'", userName)
    Dim strConnection As String = "Data Source=.\sqlexpress;Initial Catalog=Users;Integrated Security=True"
    Dim sqlConn As New SqlConnection(strConnection)
    Dim sqlDataAdap As New SqlDataAdapter(strSql, sqlConn)
    Dim dt As New DataTable()
    sqlDataAdap.Fill(dt)

    If Convert.ToInt32(dt.Rows(0)(0)) > 0 Then
        Return False
    End If

    Return True
End Function

1 个答案:

答案 0 :(得分:1)

修改您的CheckUserNameAvailability功能,从您保存所有用户名的表格中获取数据。一个例子:

Public Function CheckUserNameAvailability(userName As String) As Boolean
            Dim strSql as String = String.Format("SELECT COUNT(userNameCol) FROM users WHERE userNameCol = '{0}'", userName)
            Dim sqlConn as new SqlConnection(SQL Connection String)
            Dim sqlDataAdap as new SqlDataAdapter(strSql, sqlConn)
            Dim dt as new DataTable()
            sqlDataAdap.Fill(dt)

            If Convert.ToInt32(dt.Rows(0)(0)) > 0 Then
               Return false
            End If

            Return true
        End Function