Skip to main content
Inspiring
October 16, 2008
Answered

New to UDFs

  • October 16, 2008
  • 6 replies
  • 729 views
I am getting ready to write my first UDF.

I have a form (cfform, format flash) that is going to have many fields in which the users can type in a numeric entry (validated as float).

On blur on each field, I want to call my UDF which will do some very basic validating/formatting of the number entered into that field. (i.e., making sure there are only two numbers after the decimal for starters)

It is very (*very* lol) basic so far, I want to add in more to it after I get this initial part working. Below is what I have so far:

So the question...in my form fields, how do I call that on an onBlur event passing the current field's data to it??
This topic has been closed for replies.
Correct answer Velaria
Well...it may not be very pretty, but I managed to get it to work!

I did an onBlur call to a CFSAVECONTENT for the fields I needed to validate (see code below)

This will determine if there are only two numbers after the decimal, truncating any additional digits it finds, and also adds on the trailing zeros to make it look pretty for currency style formatting.

6 replies

Inspiring
October 22, 2008
deleted
VelariaAuthorCorrect answer
Inspiring
October 22, 2008
Well...it may not be very pretty, but I managed to get it to work!

I did an onBlur call to a CFSAVECONTENT for the fields I needed to validate (see code below)

This will determine if there are only two numbers after the decimal, truncating any additional digits it finds, and also adds on the trailing zeros to make it look pretty for currency style formatting.
Inspiring
October 18, 2008
Hi,
You can use a simple AJAX script to call your CFC for verification.
If you have some javascript experience, this should help.

cheers,
fober


==== CFM TEST SCRIPT =============================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>

<script type="text/javascript">

function ajax(valuefield, method) {

if (window.XMLHttpRequest)
request = new XMLHttpRequest();
else if (window.ActiveXObject)
request = new ActiveXObject("Microsoft.XMLHTTP");

url= "ajax.cfc?method=" + method + "&value=" + document.getElementById(valuefield).value;

request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-javascript;");
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200){
document.getElementById("status").innerHTML= request.responseText;
document.getElementById("btn_save").disabled=false;
}
}
request.send();
}

</script>

<body>

<form>
<div id="form" style="border: 1px solid blue; display: block; width: 300px;height:400px">

<input type="Text" id="value1" name="value1"
onkeyup="ajax('value1','passwordtest')"><br>
<br><br>
<input type="Text" name="value2">
<br><br>
<input type="Submit" id="btn_save" name="btn_save" value="save">
<div id="status" style="width:100%;height:30px;background-color:yellow">not tested</div>
</form>



</body>
</html>
==== CFC LIBRARY: AJAX.CFC ========================

<cfcomponent>
<cfset password_regexp= "^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).*$">
<cfset password_info= "A strong password is required (include [A-Z], [a-z], [0-9] {6})">

<cffunction access="remote" name="passwordtest" output="Yes">
<cfargument name="value" default="">
<cfif REFind(password_regexp,value) IS 0>
#password_info#
<cfelse>
Password is valid
</cfif>
</cffunction>
</cfcomponent>

VelariaAuthor
Inspiring
October 17, 2008
Hmm, back to the drawing board then! LOL (I knew it seemed to easy and straight-forward to work! heheh)

Thanks for the quick replies, I'll start looking at the actionscript or javascript routes then.
Inspiring
October 17, 2008
You cant call onBlur without any javascript code. Your best bet is to implement javascript or ajax functionality. I would suggest give it a shot to jquery, it will work nicely with UDF's and CFC's.
Inspiring
October 16, 2008
Velaria wrote:
>
> So first question...in my form fields, how do I call that on an onBlur event
> passing the current field's data to it?

You can't. Your form and all it's blurry glory are running on the
client. The UDF is on the server. They are not connected.

>
> Second question...would something like this be better suited for a CFC or some
> other method?

Not a CFC. That is also server based. Unless you do something with
AJAX to make requests to the server behind the scenes you are restricted
to what is on the client.

Uou would then be looking at a JavaScript function that runs on the
client to handle this requirement.

You may also want to investigate an ActionScript option. Since this a
flash based form, that may make some since. But it is not intuitive on
how you add custom ActionScript to the built in flash forms. I have
seen it done and it was very sophisticated, but the code was very eye
twisting. I can only suggest Googling CF Flash Forms and ActionScript
and see if you find any help.