<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dingobytes &#187; JQuery</title>
	<atom:link href="http://www.dingobytes.com/tag/jquery/feed" rel="self" type="application/rss+xml" />
	<link>http://www.dingobytes.com</link>
	<description>what your nephew can't make you</description>
	<lastBuildDate>Thu, 02 Sep 2010 05:57:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Disable submit with multiple forms present</title>
		<link>http://www.dingobytes.com/jquery/disable-submit-with-multiple-forms-present</link>
		<comments>http://www.dingobytes.com/jquery/disable-submit-with-multiple-forms-present#comments</comments>
		<pubDate>Mon, 02 Aug 2010 19:05:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[disable]]></category>
		<category><![CDATA[disabled]]></category>
		<category><![CDATA[multiple forms]]></category>
		<category><![CDATA[submit]]></category>
		<category><![CDATA[submit button]]></category>

		<guid isPermaLink="false">http://www.dingobytes.com/?p=131</guid>
		<description><![CDATA[A few minutes later I find myself scrambling through Google searches and trying different bits of code, none of which was working for me. The task was only suppose to last a few minutes and now I find myself having spent a few hours trying to figure this out. Time for a lunch break.]]></description>
			<content:encoded><![CDATA[<p>I recently ran across a situation in which I needed to disable a submit button after it was clicked to avoid duplicate submits of the form. This in itself seemed a pretty simple task to do with jQuery.</p>
<p>I soon learned that the task would be more difficult then I had anticipated. I started the task by using something similar to this.</p>
<p>
<pre class="brush: javascript">jQuery(document).ready( function() {&lt;br /&gt;
    jQuery(&#039;form&#039;).submit(function(){&lt;br /&gt;
        // On submit disable its submit button&lt;br /&gt;
        jQuery(&#039;input:submit&#039;, this).attr(&#039;disabled&#039;, &#039;disabled&#039;);&lt;br /&gt;
    });&lt;br /&gt;
});</pre>
</p>
<p>I tested and thought all would be good, only it was not. After some double checking of my spelling, I decided to just output the value of the &#8216;input:submit&#8217;. I was surprised to see that it was returning &#8216;undefined&#8217; as the value.</p>
<p>A few minutes later I find myself scrambling through Google searches and trying different bits of code, none of which was working for me. The task was only suppose to last a few minutes and now I find myself having spent a few hours trying to figure this out. Time for a lunch break.</p>
<p>While on break I decided, how about just figure this out on your own instead of trying to hack someone other developers code. Off to jQuery documentation I went and the first thing I was reminded of was that .each() returns an index. Assuming all of my forms have some type of element with type=submit, I can use this.</p>
<p>The first step was to loop through each form element on page load:</p>
<pre class="brush: javascript">jQuery(&#039;form&#039;).each(function(formIndex) {});</pre>
</p>
<p>Now inside of that each loop, lets add some logic:</p>
<pre class="brush: javascript">jQuery(this).submit(function() {&lt;br /&gt;
    jQuery(&#039;input:submit&#039;).eq(formIndex).attr(&#039;disabled&#039;, &#039;disabled&#039;);&lt;br /&gt;
});</pre>
</p>
<p>This did the trick. I was now able to disable just the one form. Now if I was using this on AJAX script I would just add a return false. Here is the entire code, assuming we are making this an AJAX call.</p>
<p>
<pre class="brush: javascript">jQuery(document).ready(function() {&lt;br /&gt;
	//Disable all form submit button on click?&lt;br /&gt;
	jQuery(&#039;form&#039;).each(function(formIndex) {&lt;br /&gt;
		jQuery(this).submit(function() {&lt;br /&gt;
			jQuery(&#039;input:submit&#039;).eq(formIndex).attr(&#039;disabled&#039;, &#039;disabled&#039;);&lt;br /&gt;
			return false;&lt;br /&gt;
		});&lt;br /&gt;
	});&lt;br /&gt;
});</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dingobytes.com/jquery/disable-submit-with-multiple-forms-present/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Show Message 1.0- jQuery Plugin for displaying messages</title>
		<link>http://www.dingobytes.com/tutorial/show-message-1-0-jquery-plugin-for-displaying-messages</link>
		<comments>http://www.dingobytes.com/tutorial/show-message-1-0-jquery-plugin-for-displaying-messages#comments</comments>
		<pubDate>Mon, 01 Feb 2010 02:26:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[notification box]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.dingobytes.com/?p=90</guid>
		<description><![CDATA[Inspired by a similar feature at Twitter.com, this jQuery Plugin will help you display messages at the top of your page with an assortment of options. Download the latest version 2.1 of the plugin at http://showMessage.dingobytes.com/download/ Please see the site, as the information below is for previous versions. This has been updated! This is my [...]]]></description>
			<content:encoded><![CDATA[<p>Inspired by a similar feature at Twitter.com, this jQuery Plugin will help you display messages at the top of your page with an assortment of options.</p>
<p style="border:medium #06f solid;width:95%;padding:0.5em;font-size:x-large;text-align:center;">Download the latest version 2.1 of the plugin at <a href="http://showMessage.dingobytes.com/download/" title="download">http://showMessage.dingobytes.com/download/</a></p>
<p><strong><em>Please see the site, as the information below is for previous versions. This has been updated!</em></strong></p>
<p>This is my first attempt at a jQuery Plugin, so although it works fine for my liking, I am sure there are many things that can be done more effectively.</p>
<ul>
<li>Message displayed at the top of page (no scrolling to view error)</li>
<li>Multiple messages can be displayed</li>
<li>Options to automatically close the message after set delay</li>
<li>Message closes with blur/&#8217;esc&#8217; key/&#8217;close&#8217; link</li>
</ul>
<ul>
<li><a title="Download" href="#download">Download</a></li>
<li><a title="Install" href="#install">Install</a></li>
<li><a title="Options" href="#options">Options</a></li>
<li><a title="Examples" href="#examples">Examples</a></li>
</ul>
<div id="download">
<h2><span id="more-90"></span>Download</h2>
<p style="border: medium #06f solid; width: 95%; padding: 0.5em; font-size: x-large; text-align: center;"><a title="download now" href="http://showMessage.dingobytes.com/download/" target="_blank">Download showMessage 1.1</a></p>
</div>
<div id="install">
<h2>Install</h2>
<p>You can install showMessage jQuery Plugin by including the script somewhere in the page AFTER jQuery. This plugin should work on jQuery version 1.3.2+.</p>
<pre class="brush: html">&lt;script type=&quot;text/javascript&quot; src=&quot;/path/to/jquery.showMessage.min.js&quot;
charset=&quot;utf-8&quot;&gt;&lt;\/script&gt;</pre>
<p>If you wish to style the text, the message is displayed through an unordered list in the div with id attribute=showMessage</p>
<pre class="brush: css">#showMessage li
{
font-family: Arial, Helvetica, clean, sans-serif;
font-size: large;
font-weight: bold;
color: red;
}</pre>
</div>
<div id="options">
<h2>Options</h2>
<p>Here are the possible options with their default values:</p>
<pre class="brush: javascript">
thisMessage:		[&#039;&#039;],
backgroundColor:	&#039;#F7F7F7&#039;,
color:			&#039;#000000&#039;,
opacity:		90,
displayNavigation:	true,
autoClose:		false,
delayTime:		5000
</pre>
<dl>
<dt>thisMessage <em>(required)</em><span class="alignright">[Array]</span></dt>
<dd>The message you will be returning to the page. The message is in the form of an array.</dd>
<dt>blackgroundColor<span class="alignright">[string]</span></dt>
<dd>Default set to &#8216;#F7F7F7&#8242;. The background color of the message holder. (&#8216;#F7F7F7&#8242; or &#8216;rgb(247, 247, 247)&#8217; or &#8216;white&#8217;)</dd>
<dt>color<span class="alignright">[string]</span></dt>
<dd>Default set to &#8216;#000000&#8242;. The font color in the message holder. (&#8216;#000000&#8242; or &#8216;rgb(0, 0, 0)&#8217; or &#8216;black&#8217;)</dd>
<dt>opacity<span class="alignright">[integer]</span></dt>
<dd>Default set to 90. Opacity of the message holder. Value is an integer between 1 and 100.</dd>
<dt>displayNavigation<span class="alignright">[boolean]</span></dt>
<dd>Default set to true. Will display the &#8216;esc&#8217; and &#8216;close&#8217; information in upper right.</dd>
<dt>autoClose<span class="alignright">[boolean]</span></dt>
<dd>Default is set to false. When set to true, it will close the message holder after specified delay time.</dd>
<dt>delayTime<span class="alignright">[integer]</span></dt>
<dd>Default is set to 5000. Amount of time in milli-seconds before the message holder will close/hide (autoClose must be set to true).</dd>
</dl>
</div>
<div id="examples">
<h2>Example</h2>
<dl>
<dt>Simple</dt>
<dd> Just return a simple message to message holder.</p>
<pre class="brush: javascript">
jQuery.showMessage({
&#039;thisMessage&#039;:[&#039;&lt;mg src=&quot;/images/accept.png&quot; alt=&quot;accept&quot;/&gt;successful!&#039;]
});
</pre>
</dd>
<dt>Advanced</dt>
<dd> This should look a lot like Twitter.</p>
<pre class="brush: javascript">
var returnMessage = [&#039;Account was created successfully!&#039;,&#039;Thank you.&#039;];
jQuery.showMessage({
&#039;thisMessage&#039;:		returnMessage,
&#039;backgroundColor&#039;:	&#039;#f7f7f7&#039;,
&#039;color&#039;:		&#039;black&#039;,
&#039;opacity&#039;:		95,
&#039;displayNavigation&#039;:	false,
&#039;autoClose&#039;:		true,
&#039;delayTime&#039;:		6000
});
</pre>
</dd>
</dl>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.dingobytes.com/tutorial/show-message-1-0-jquery-plugin-for-displaying-messages/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>jQuery username check</title>
		<link>http://www.dingobytes.com/tutorial/jquery-username-check</link>
		<comments>http://www.dingobytes.com/tutorial/jquery-username-check#comments</comments>
		<pubDate>Tue, 29 Sep 2009 19:04:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Coldfusion]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.dingobytes.com/?p=6</guid>
		<description><![CDATA[There are many times we might want to retrieve some data from the server or database without taking the user away from the current page. One such example of this is when a user is registering for a website and they need to verify if a username is available for use or not. Using an [...]]]></description>
			<content:encoded><![CDATA[<p>There are many times we might want to retrieve some data from the server or database without taking the user away from the current page. One such example of this is when a user is registering for a website and they need to verify if a username is available for use or not. Using an AJAX XMLHttpRequest is a quick and efficient way to check the availability of the username. The javascript XMLHttpRequest retrieves information from the server without using a page refresh.</p>
<p><span id="more-6"></span>If you are not familiar with how JavaScript uses a generic XMLHttpRequest function, we would strongly suggest you read one of the many tutorials out there. Once you understand how it works and the different methods employed to do it, you will appreciate the ease of jQuery even that much more.</p>
<p>The first step is to download a current version of <a title="latest jQuery at time of writing" href="http://docs.jquery.com/Release:jQuery_1.3.2" target="_blank">jQuery</a>. We prefer to use the minified version. Another option is to load jQuery into your page is to use the <a title="Google AJAX Libraries API" href="http://code.google.com/apis/ajaxlibs/documentation/" target="_blank">Google AJAX Libraries API</a>. We don&#8217;t use the other AJAX libraries enough to prefer this method, but we have used it in the past for client sites.</p>
<p>Of course we like things to be pretty, so we would also suggest some good images/icons to represent a valid and invalid username. You can create your own image or simply use <a title="Free Icon Set search" href="http://images.google.com/images?hl=en&amp;client=opera&amp;rls=en&amp;um=1&amp;sa=1&amp;q=free+icon+sets&amp;aq=f&amp;oq=&amp;aqi=g1&amp;start=0" target="_blank">Google Images</a> to find a nice free icon set that has something to represent this. We have attached two simple icons that we prefer to use.</p>
<p>To get the document started, we assume that you have jQuery loading somewhere in the head of the document (using which ever method you prefer). We are going to make this simple so we just need to create a form to enter a username. The rules for a username are as follows:</p>
<ol>
<li>Username can not have whitespace</li>
<li>Username must be at least 3 characters long</li>
<li>Username must be no longer then 24 characters long</li>
</ol>
<h2>Simple Registration Form</h2>
<p>
<pre class="brush: html">&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;&lt;form id=&quot;registrationForm&quot; action=&quot;/test/registrationForm.php&quot; method=&quot;post&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;fieldset&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;legend&gt;Use form below to register&lt;/legend&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;/fieldset&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;dl&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;dt&gt;&lt;label for=&quot;username&quot;&gt;Username&lt;/label&gt;&lt;/dt&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot; /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;Your username must be alphanumeric, 3-24 characters long, can contain underscores, but no spaces. We recommend that you don&#039;t change this unless you really have to.&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;dt&gt;&lt;label for=&quot;passwd&quot;&gt;Password&lt;/label&gt;&lt;/dt&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;dd&gt;&lt;input type=&quot;password&quot; id=&quot;passwd&quot; name=&quot;passwd&quot; /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;dt&gt;&lt;label for=&quot;passwd-confirm&quot;&gt;Confirm Password&lt;/label&gt;&lt;/dt&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;dd&gt;&lt;input type=&quot;password&quot; id=&quot;passwd-confirm&quot; name=&quot;passwd-confirm&quot; /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;dt&gt;&lt;/dt&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;input type=&quot;submit&quot; id=&quot;submitRegistration&quot; value=&quot;Register&quot; /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;/dl&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;/form&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
</pre>
</p>
<p>Now that we have our form setup, we need to do a few things. Obviously we want to check the username when it is entered (or being entered). Second we will want to have some sort of client side validation to save us from processing an incomplete form. Lastly we will want to submit the form and process the response from the server. Whether you use PHP, ASP, ColdFusion or any other method, it is a good practice to validate the form server side as well. We won&#8217;t go into that to much, but is a point worth mentioning.</p>
<p>We prefer to insert the following BEFORE the form. You can put this in a *.js file or you can simply insert it into the page, either works for us. We will need to break the JavaScript into three parts. One function that checks the username, a second function that reads the username input element and initiates username check and a third function to validate and process the form.</p>
<h2>Check Username Function</h2>
<p>
<pre class="brush: jscript">&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;&lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
//&lt;![CDATA[&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
function usernameCheck(username)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
$.get(&#039;/test/usernameCheck.php&#039;, { &#039;username&#039;:username }, function(xml) {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
$(xml).find(&#039;response&#039;).each( function() {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var error = $(this).find(&#039;error&#039;).text();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
if ( error == &#039;false&#039; )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var successCheck = $(&#039;&lt;span&gt;&lt;/span&gt;&#039;)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
.css({&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&#039;padding-left&#039;:&#039;1em&#039;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
})&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
.html(&#039;&lt;img src=&quot;/test/images/dialog-ok.png&quot; alt=&quot;successful&quot; /&gt;Username is available.&#039;);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
$(&#039;input[name=username]&#039;).after(successCheck);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
else&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var errorCheck = $(&#039;&lt;span&gt;&lt;/span&gt;&#039;)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
.css({&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&#039;padding-left&#039;:&#039;1em&#039;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
})&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
.html(&#039;&lt;img src=&quot;/test/images/dialog-error-small.png&quot; alt=&quot;error&quot; /&gt;Username is already registered.&#039;);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
$(&#039;input[name=username]&#039;).after(errorCheck);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
});&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
});&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
</pre>
</p>
<p>The code might look overwhelming right now, but it is actually pretty simple. If we break it down, you will see that the first thing we do inside the  usernameCheck() function is call the jQuery AJAX function <em><a title="jQuery $.get AJAX function" href="http://docs.jquery.com/Ajax/jQuery.get#urldatacallbacktype" target="_blank">$.get</a></em>. This jQuery function will load a remote page using an HTTP GET request. It only has one requirement (the URL), but also allows for [data], [callback], [type] to be included. We are using both [data] and[callback], with [data] = { &#8216;username&#8217;:username } and [callback] = function(xml). The callback is the function that we want to be executed whenever the data is loaded successfully.</p>
<p>Looking into the function you will see that we are finding each &lt;response&gt; tag in the XML repsponse and inside of that we are looking for the text inside the &lt;error&gt;&lt;/error&gt; node/element of the XML and setting it to the variable &#8220;error&#8221;.</p>
<p>If the XML responds that error is &#8220;false&#8221; we use jQuery to create a &lt;span&gt;&lt;/span&gt; element with image and text response inside of it indicating the username is available. We then insert the new element we have just created after the form input element for username.</p>
<p>The &#8220;else&#8221; is doing the same thing, only it is showing the username is not available.</p>
<p>We will add some more components to this later, but for now lets move to the next functions.  These two functions should be triggered after the page loads, so we will be placing them inside the $(function() {}); element which tells jQuery to load them after the page loads. There are other methods to do this same thing (such as $(document).ready(function(){}); so if you are familiar with jQuery you can use the element you feel most comfortable with.</p>
<h2>Initiate Username Check</h2>
<p>
<pre class="brush: jscript">&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;$(function() {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var t;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
$(&#039;input[name=username]&#039;).keyup( function() {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
// REMOVE WHITESPACE FROM USERNAME&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var username = $(&#039;input[name=username]&#039;).val();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
username = username.replace(/\s/g,&#039;&#039;);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
$(&#039;input[name=username]&#039;).val(username);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
// IF THE USERNAME VALIDATES, CHECK THE DB&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
if ( username !== &#039;&#039; &amp;amp;amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp;amp;amp; ( username.length &gt;= 3 &amp;amp;amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp;amp;amp; username.length &lt;= 24 ) )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
if ( typeof(t) != &#039;undefined&#039; )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
clearTimeout(t);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
t = setTimeout( function() { usernameCheck(username) }, 750 );&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
});&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
</pre>
</p>
<p>You will notice the first variable we define is t. This is going to be used for the setTimeout() function later.</p>
<p>After that declaration you will see we are asking jQuery to watch the keyup event associated with the &#8220;username&#8221; input field and do &#8220;stuff&#8221; after each keystroke. We start  by simply asking for the value of the input field. Then we use the javascript replace() function to strip out any whitespace values followed by reinserting the &#8220;new&#8221; username back into the field.</p>
<p>We don&#8217;t want to even check for the username unless it meets the criteria we mentioned earlier, so we use the if() statement to help us validate that part. If the username is blank or if the username is greater then or equal to 3 characters and the username is less then or equal to 24 characters, then we continue to process the username.</p>
<p>We don&#8217;t want to bombard our server with a request every time a keystroke is made so we use the setTimeout function to  delay the request by a certain amount of time. In order to do this correctly though, we also need to clear the timeout if another keystroke is made within the delay, in effect starting the timeout over again.</p>
<p>Knowing this, we need to give the setTimeout a variable (t) when we call it. Because we know t may or may not exist, we use the if () statement to check for it. If it is defined, we use clearTimeout() function to remove the setTimeout. We can then set the variable t to a fresh setTimeout delay.</p>
<h2>Validate and Submit Form</h2>
<p>When the end user submits the form, we want to validate it and if it passes validation, submit the form. I am not going to go into a lot of detail about this as I have already done similar tutorials (<a title="showMessage tutorial" href="http://www.dingobytes.com/tutorial/showmessage">see here</a>). In a nutshell, we are just making sure the form is filled out and using AJAX to tell us if we were successful/unsuccessful when submitting form.</p>
<p>
<pre class="brush: jscript">&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;$(&#039;#submitRegistration&#039;).click( function() {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
//validate the form and submit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var username = $(&#039;input[name=username]&#039;).val();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var passwd = $(&#039;input[name=passwd]&#039;).val();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var passwd-confirm = $(&#039;input[name=passwd-confirm]&#039;).val();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var registrationData = $(&#039;#registrationForm).serializeArray();&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;if ( username === &#039;&#039;  || ( username !== &#039;&#039; &amp;amp;amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp;amp;amp; ( username.length &lt; 3 || username.length &gt; 24 ) ) )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
errorMessage.push(&quot;Username must have between 3 and 24 characters.&quot;);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
if ( username !== &#039;&#039; &amp;amp;amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp;amp;amp; /\s/.test( username ) )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
errorMessage.push(&quot;Username can not have any spaces.&quot;);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
if ( passwd != passwd-confirm )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
errorMessage.push(&quot;Password and Password confirmation do not match.&quot;);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;if ( errorMessage.length &gt; 0 )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
showMessage( errorMessage, &quot;error&quot;, &quot;alert&quot; );&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
return false;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
else&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
$.post(&#039;/test/registrationForm.php&#039;, registrationData, function(xml) {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
$(xml).find(&#039;response&#039;).each( function() {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var error = $(this).find(&#039;error&#039;).text();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var returnMessageString = $(this).find(&#039;returnMessage&#039;).text();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
var returnMessage = [];&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
if ( error == &#039;false&#039; )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
returnMessage.push(returnMessageString);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
showMessage( returnMessage, &quot;highlight&quot;, &quot;info&quot; );&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
else&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
returnMessage.push(returnMessageString);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
showMessage( returnMessage, &quot;error&quot;, &quot;alert&quot; );&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
});&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
});&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
return false;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
});&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
</pre>
</p>
<p>So that is our start. You will notice if you run this code that it fails to work properly. What we don&#8217;t have is a way to clear out the &#8220;response&#8221; if it already exists on the page. Because we know where the response should be, we can create a simple check for the response and if it is found, remove it.  You will need to do this in both the usernameCheck function and also the function that sets the timeout.</p>
<p>
<pre class="brush: jscript">&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;if ( $(&#039;input[name=username]&#039;).next(&#039;span&#039;).length )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
$(&#039;input[name=username]&#039;).next(&#039;span&#039;).remove();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
</pre>
</p>
<p>Using the length of an element is how we check for the elements existence when using jQuery. Once we find that it does exist, we remove the entire element using the jQuery remove() function.</p>
<p>Lastly, jQuery has some features built in that make our forms prettier. I like to use animation to soften the effect of having elements just appearing BLAM! on the page. We can add a slow fade in effect to the username check to ease the message on the page . We add a diplay=none style to the span and then trigger the jQuery fadeIn() effect to achieve this.</p>
<p>
<pre class="brush: jscript">&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;var successCheck = $(&#039;&lt;span&gt;&lt;/span&gt;&#039;)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
.css({&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&#039;display&#039;:&#039;none&#039;,&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&#039;padding-left&#039;:&#039;1em&#039;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
})&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
.html(&#039;&lt;img src=&quot;/test/images/dialog-ok.png&quot; alt=&quot;successful&quot; /&gt;Username is available.&#039;);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
$(&#039;input[name=username]&#039;).after(successCheck);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
$(successCheck).fadeIn(&#039;slow&#039;);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
</pre>
</p>
<p>There you have it, a nice jQuery username check.  You can test this out here or you can download the files (all zipped up) and give it a try yourself.</p>
<p><a title="usernameCheck.zip" href="http://dingobytes.com/example/usernameCheck.zip">usernameCheck.zip</a> (124KB)<br />
<a title="usernameCheck.rar" href="http://dingobytes.com/example/usernameCheck.rar">usernameCheck.rar</a> (117KB)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dingobytes.com/tutorial/jquery-username-check/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
