Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • 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
 
sam2912Commented:

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
 
sam2912Commented:
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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