Previous Article
Twitter Style Load More Results with jQuery and Ajax in Classic ASP & Access (or) SQL Server
Check if usename is taken (ASP Classic and JQuery)
Next Article
Ajax pagination using ASP Classic jQuery and SQL Server / Access Database
Coding Article #: 11 - Published On: May 11, 2011 @ 01:25:46 AM - Last Updated on: July 13, 2012
This article has been Favorited 45 times
Join today, and add this to your favorites.
Coding Source - Share on MySpace Coding Source - Share With Facebook Coding Source - Share on Twitter Coding Source - Share on Reddit Coding Source - Share on Digg It Coding Source - Share on Stumble Upon It Coding Source - Share on Delicious
Share With Friends (Updated 6-8-2010)

Supported Files
File Download:
carrzkiss_013006Availability.zip
File Size: 14.69 KB
Unzip with
Download winzip today!(OR)
Screenshot of Source Code
Coding Source - Check if usename is taken (ASP Classic and JQuery)
Using this function in your Classic ASP pages, you will be able to allow your visitors to check and see if the username is available of not before proceeding.

View the code below and download the project file with
Access Database
Connection Script
Database Scripts

Example: View Live Example«

#1: We connect to our database

<%
Set objConn = CreateObject("ADODB.Connection")
objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath ("Availability.mdb") & ";"
objConn.Open

' OR
objConn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" & _
Server.MapPath ("Availability.mdb")
%>


#2: We secure out code, so that no one can send XSS (or) SQL Injections
This will be used in our next steps.

<%
Function ProtectSQL(SQLString)
SQLString = Replace(SQLString, "'", "''") ' replace single Quotes with Double Quotes
SQLString = Replace(SQLString, ">", ">") ' replace > with >
SQLString = Replace(SQLString, "<", "<") ' replace < with <
SQLString = Replace(SQLString, "(","(") ' replace ( with (
SQLString = Replace(SQLString, ")",")") ' replace ) with )
SQLString = Replace(SQLString, "&", "&")
SQLString = Replace(SQLString, "", "")
SQLString = Replace(SQLString, "%", "%")
SQLString = Trim(SQLString)
ProtectSQL = SQLString
End Function
%>


#3: Javascript has to be used, in order to work with our database in a realtime environment.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

		//the min chars for username
		var min_chars = 3;

		//result texts
		var characters_error = 'Minimum amount of chars is 3';
		var checking_html = 'Checking...';

		//when button is clicked
		//$('#check_username_availability').click(function(){
		//when keyup check
		$("#username").keyup(function(){
			//run the character number check
			if($('#username').val().length < min_chars){
				//if it's bellow the minimum show characters_error text '
				$('#username_availability_result').html(characters_error);
			}else{
				//else show the cheking_text and run the function to check
				$('#username_availability_result').html(checking_html);
				check_availability();
			}
		});

  });

//function to check username availability
function check_availability(){

		//get the username
		var username = $('#username').val();

		//use ajax to run the check
		$.post("check_username.asp", { username: username },
			function(result){
				//if the result is 1
				if(result == 1){
					//show that the username is available
					$('#username_availability_result').html(username + ' is Available');
				}else{
					//show that the username is NOT available
					$('#username_availability_result').html(username + ' is not Available');
				}
		});

}
</script>


#4: Create our form, with a DIV tag, with the same name for the JS code above.

<form>
<input type="text" id="username"> 
<input type="button" id="check_username_availability" value="Check Availability">
<div id="username_availability_result"></div>
</form>


#5: The last thing we need to do, on the processing page.
If you noticed from step 4 above, the form did not have a method nor an action, that is because the JS code handles everything for us.
However, to use this code in a live site, you will of course need to have the method and action in your form, to process what is in the text fields.

<%
' This function is used to make sure that if we type in:
' secret <= Available
' secrets <= not Available
' See the 1 character difference between the 2 names? The escape() function will fix this for us.
function escape(InputString)
dim a
a=InputString
a = Replace(a,"\x00","\\x00") 
a = Replace(a,"\n","\\n") 
a = Replace(a,"\r","\\r") 
a = Replace(a,"\\","\\\\") 
a = Replace(a,"\n","\\n") 
a = Replace(a,"\n","\\n") 
a = Replace(a,"\","\\\")
a = Replace(a,"\x1a","\\\x1a")
escape=a
end function

' First we need to get our QueryString from our AJAX Function, and then Wrap it with out ProtectSQL() ASP Function.
username = ProtectSQL(escape(request("username")))
Set chUN = Server.CreateObject("ADODB.Command") ' Creates our Server Object
chUN.ActiveConnection=objConn ' Connects to our Database
chUN.Prepared = true ' Lets us know that it is ready to process
' The next step is to gather the Columns from our database and then Query the Username=? column.
chUN.commandtext="SELECT UNID, Username from Usernames where Username=?"
' Next, we are going to use the Parameter to make sure that are code is protected again. This is the main protection, to where the ProtectSQL() Function is used as a Double Coated Protection against people how what to attempt to hack us.
chUN.Parameters.Append chUN.CreateParameter("@Username", 200, 1, 25, username) 
' Next, we SET our RecordSet and execute our SQL.
set rsUN = chUN.execute
' The below steps: rsUN.eof checks the database to see rather the record exist or now. 

counter = 0
while not rsUN.eof
    counter = counter + 1
rsUN.movenext
wend
if not rsUN.eof then
rsUN.movefirst
end if


if counter>0 then
response.write 0
else
response.write 1
end if
end if
%>
Post to Facebook about: Check if usename is taken (ASP Classic and JQuery)