• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 679
  • Last Modified:

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

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="">
<fieldset>
<label for="name"><span>Full Name</span>
<input type="text" id="name" name="name" class="input" />
</label>
<div class="message grey">Enter your Name</div>
							
							<label for="username"><span>Username</span>
<input type="text" id="username" name="username" class="input" />
</label>
<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" />
</label>
						
							<input type="submit" value="REGISTER" class="submit" />
</fieldset>
</form>

Open in new window

0
RedBullLee
Asked:
RedBullLee
1 Solution
 
duncancummingCommented:
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:
http://jqueryfordesigners.com/using-ajax-to-validate-forms/

If you go down the jQuery route you might want to use the validation plugin with it.
http://docs.jquery.com/Plugins/Validation
0
 
sandy12879Commented:
<input type="text" id="username" name="username" class="input" />
<span></span>
 
<script language="javascript" type="text/javascript">
    jQuery(function(){
      jQuery('#username')
        .data('timeout', null)
        .keyup(function(){
            clearTimeout(jQuery(this).data('timeout'));
            jQuery(this).data('timeout', setTimeout(formname, 2000));
        });
    });
</script>

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
0
 
reitersCommented:
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');
							}
						}
					});
				break;
			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');
							}
						}
					});
				break;
			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');
							}
						}
					});
				break;
			}
		}
	
	function form_submit() {
		if (name_ok && user_ok && email_ok) {
			submit();
			}
		else {
			alert('you must correctly fill out the entire form');
			}
		}
</script>

<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();">
</form>






<!-- 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>
	<cfcase value="uid">
		<cfquery name="foo">
			select count(uid) as user_count
			from user_table
			where uid = '#url.user_value#'
		</cfquery>
		<cfif foo.user_count eq 0><cfset result = 1></cfif>
	</cfcase>
	<cfcase value="email">
		<cfif isvalid('email',url.user_value)><cfset result = 1></cfif>
	</cfcase>
</cfswitch>

<cfoutput>#result#</cfoutput>

Open in new window

0
 
RedBullLeeAuthor Commented:
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now