开发者

Instant Username check availablity with database using asp.net

开发者 https://www.devze.com 2023-04-06 03:54 出处:网络
I am trying to use one of the best example for checking the username availability from the below site

I am trying to use one of the best example for checking the username availability from the below site http://www.highoncoding.com/Articles/439_Performing_Instant_UserName_Availability_Check_Using_JQuery_Ajax_API.aspx

And It's just verifying with some pre-initiated names but I wan't to try with my database can anyone suggest me how to proceed further.

Here is the code:

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

This 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

Modified code:

 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


Modify your CheckUserNameAvailability function to get data from the table where you have saved all the username. An example:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消