validate username ajax and json and asp.net?

mvc333

New member
I am trying to validate a usernames existance on a webpage without a page referesh using ajax

I have the following html file

<!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">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
<!--

$(document).ready(function () {
var validateUsername = $('#validateUsername');
$('#username').keyup(function () {
var t = this;
if (this.value != this.lastValue) {
if (this.timer) clearTimeout(this.timer);
validateUsername.removeClass('error').html('<img src="images/ajax-loader.gif" height="16" width="16" /> checking availability...');
this.timer = setTimeout(function () {
var username = document.getElementById('username').value
$.getJSON('ValidateUsername.aspx?username=' + username, function (results) {
alert(username)
if (results.available) {
validateUsername.html('Yay!');
}
else {
validate.html('BOOO');
}
})
}, 200);
this.lastValue = this.value;
}
});
});
//-->
</script>

</head>
<body>
<fieldset>
<div>
<label for="username">Username, valid: a-z.-_</label>
<input type="text" name="username" value="" id="username" />
<span id="validateUsername"></span>
</div>
</fieldset>
</body>
</html>

it is calling an asp.net page to get the results from a query to see if a username is available

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UserSite.DataClasses;
using System.Data;

namespace OohruWeb
{
public partial class ValidateUsername : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/json";
string NameToLookUp = Request.QueryString["username"];
if (NameToLookUp == null) {
NameToLookUp = "";
}
DbaseExecSpWithReturnValue Sproc = new DbaseExecSpWithReturnValue();
Sproc.SetSp("sp_CheckUsernameAvailable");
Sproc.AddParam(1);
Sproc.AddParam("Username",SqlDbType.Char,NameToLookUp,20);
int RetVal = Sproc.Execute();
Sproc.Close();
if (RetVal == 0)
Response.Write(@"'{""success"": false}'");
if (RetVal == 1)
Response.Write(@"'{""success"": true}'");
}
}
}


Now i know it is working on the server side because my stored procedure is making a log into a special table when it is called. It is making a log entry with the proper username being requested. However it is not even giving me the alert in the html pageshown here
$.getJSON('ValidateUsername.aspx?username=' + username, function (results) {
alert(username)

Please let me know if you spot my problem!!
 
Back
Top