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

How to pass a javascript variable to Cold fusion

I am using a jquery/ajax function to check a username for availability in a database table.  It is working great... but I need to know how to validate the form that the username field resides in.  Currently, if the cfc returns true... it adds text in a div (css driven). Perfect, but if the username is not available, I do not want the user to be able to submit the form.

Obviuosly I can validate forms, but how to do it in this instance??

Any help appreciated...
check.cfm:
_________________________

<script type="text/javascript">
chkUsernameUnique = function(theUsername){
	$.getJSON("/myCFC.cfc", {
		method: 'chkUsername',
		Username: theUsername,
		returnformat: 'json'
	}, function(isUsernameUnique){
	
		if (isUsernameUnique == true) {
			$("#theErrorDivID").html('Please select a new username, that one is taken');
		}
		else {
			$("#theErrorDivID").html('Username is available');
		}
	});
};
</script>

<div id="theErrorDivID"></div>
<input type="text" name="username" id="username" onchange="chkUsernameUnique(this.value);" />


++++++++++++++++++++++++++++++++++++++++++++

.cfc
_____________________
<cfcomponent>
<cffunction name="chkUsername" access="remote" returnformat="json" output="false">
		<cfargument name="Username" required="true">
			
		<cfquery name="chkUsername" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
		SELECT cst_ID FROM tbl_customers WHERE cst_username = <cfqueryparam value="#arguments.Username#" cfsqltype="cf_sql_varchar" />
		</cfquery>
		
		<cfreturn yesNoFormat(chkUsername.recordCount)>
	</cffunction>
</cfcomponent>

Open in new window

0
14_east
Asked:
14_east
  • 2
  • 2
1 Solution
 
Samuel LiewCommented:

if (isUsernameUnique == true) {
	$("#theErrorDivID").html('Please select a new username, that one is taken');
	$("input[type=submit]").attr("disabled", true);
}
else {
	$("#theErrorDivID").html('Username is available');
	$("input[type=submit]").attr("disabled", false);
}

Open in new window

0
 
14_eastAuthor Commented:
Hell yea, thanks man!!
0
 
14_eastAuthor Commented:
Uh-oh... I spoke to soon.  It works fine, but I realized that my "submit" button is an image foeld.   will that make a difference?
0
 
Samuel LiewCommented:
It should not make a difference. However, if using an image, user may not know that the submit button is disabled as it looks the same, disabled or not.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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