Implementing a 'live' username check during signup form with jQuery & Coldfusion

Posted on 2010-09-14
Medium Priority
Last Modified: 2012-05-10
Hi Folks,

I've tried a few scripts I've found online, however I'm struggling to make any of them work for me.  I have a simple form with 3 fields - name, username and email.  I need to accomplish the following;

- Validate input on the name field (this can be as simple as making sure it's not left empty)

- Check username as it's entered - I'm thinking something very similar to the twitter signup form here.  I have a div element alongside that holds the message, with various classes to style it.  So, initially I would show a div with 'grey' class and a message 'choose a username'.  As the user types, the database is checked to see if the name is already taken and returns the message and div styling as appropriate.  If the name is available, I also have text below this I'd like to update to show how the username will be displayed (see form code/layout below for explanation).

- Validate Email address is correctly formatted.

- Disable the form submit until all of the above are ok

I can accomplish the validation and username checks *after* form submit (using simple <cfif>, but I'm struggling to figure out how to do this before form submit.

Any pointers much appreciated!
<!--- Current Form --->
<form action="">
<label for="name"><span>Full Name</span>
<input type="text" id="name" name="name" class="input" />
<div class="message grey">Enter your Name</div>
							<label for="username"><span>Username</span>
<input type="text" id="username" name="username" class="input" />
<div class="message grey">Choose Username</div>
<p>Your public profle will be http://www.site.com/CHOSENUSERNAME</p>
							<label for="email"><span>Your Email</span>
							<input type="text" id="email" name="email" class="input" />
							<input type="submit" value="REGISTER" class="submit" />

Open in new window

Question by:RedBullLee
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 16

Expert Comment

ID: 33679333
Ok, you're going to have to use Javascript to achieve the client-side validation.  One way might be to use the jQuery framework.  Or there are other frameworks, or you could try using ColdFusion's built-in validation.  Similarly with the username check, you'll need to do an AJAX request to find out if it's taken.

Here's a nice example:

If you go down the jQuery route you might want to use the validation plugin with it.

Expert Comment

ID: 33679447
<input type="text" id="username" name="username" class="input" />
<script language="javascript" type="text/javascript">
        .data('timeout', null)
            jQuery(this).data('timeout', setTimeout(formname, 2000));

Not tried the above piece of code but your approach should be on KEY up if you want to validate the code without submitting the code.
Let me know if it helps

Accepted Solution

reiters earned 2000 total points
ID: 33693083
I wrote this with no testing so I'm sure it full of typos.  It's just to give the general idea.  I can only attach one snippet so this is 2 files in one snippet.
<!-- html page -->

<script type="text/javascript">
	name_ok = 0;
	user_ok = 0;
	email_ok = 0;
	function test_input(check) {
		switch(check) {
			case 'name':
				$.ajax({ url: 'test_form.cfm?field=name&user_value=' + escape($('#name').val()),success: cache: false, function(result){
						if (result == 1) {
							name_ok = 1;
							$('#name_response').html('good name');
						else {
							name_ok = 0;
							$('#name_response').html('bad name');
			case 'uid':
				$.ajax({ url: 'test_form.cfm?field=uid&user_value=' + escape($('#uid').val()),success: cache: false, function(result){
						if (result == 1) {
							uid_ok = 1;
							$('#uid_response').html('good uid');
						else {
							uid_ok = 0;
							$('#uid_response').html('bad uid');
			case 'email':
				$.ajax({ url: 'test_form.cfm?field=email&user_value=' + escape($('#email').val()),success: cache: false, function(result){
						if (result == 1) {
							email_ok = 1;
							$('#email_response').html('good email');
						else {
							email_ok = 0;
							$('#email_response').html('bad email');
	function form_submit() {
		if (name_ok && user_ok && email_ok) {
		else {
			alert('you must correctly fill out the entire form');

<form action="foo.cfm" method="post">
	name: <input type="text" name="name" id="name" value="" onchange="test_input('name')"><span id="name_response"></span><br />
	user id: <input type="text" name="uid" id="uid" value="" onchange="test_input('uid')"><span id="uide_response"></span><br />
	email: <input type="text" name="email" id="email" value="" onchange="test_input('email')"><span id="email_response"></span><br />
	<input type="button" value="Submit" onclick="form_submit();">

<!-- test_form.cfm --->

<cfsetting enablecfoutputonly="yes">

<cfparam name="url.field" default="oops">
<cfparam name="url.user_value" default="">

<cfset result = 0>

<cfswitch expression="#url.field#">
	<cfcase value="name">
		<cfif len(url.user_value) gt 0><cfset result = 1></cfif>
	<cfcase value="uid">
		<cfquery name="foo">
			select count(uid) as user_count
			from user_table
			where uid = '#url.user_value#'
		<cfif foo.user_count eq 0><cfset result = 1></cfif>
	<cfcase value="email">
		<cfif isvalid('email',url.user_value)><cfset result = 1></cfif>


Open in new window


Author Comment

ID: 33719201
Apologies to all for the lateness of my reply - I had to go away for a few days.

Before leaving, I managed to come up with something that worked using cfdiv binds - not ideal, but did the trick.

Since returning, I've gone through the answers here and reiters suggested code brought a few improvements to my own.

I will therefore select that as the solution.  Thanks to all for your input.

Featured Post

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question