views:

44

answers:

3

Is it possible to send a javascript function to test if a form field was changed then pass a boolean (bCheckField) to a filter function that will tell it to actually perform the check or to skip over the validation and continue? If so, how should I approach this?

Utilizing Coldfusion 8, MachII and Microsoft SQL.

Thank you very much...

+2  A: 

The easiest way, as far as I know, would be to check your checkbox (or populate your hidden field, etc.) when an onChange() fires on the target field.

On the other hand, if this is to insert/update a SQL field, I probably wouldn't trust the JS to allow the user to skip validation (unless it also skips the insert/update). You could always store the original field data in a persistent scope and do a comparison server-side when the form is returned.

Ben Doom
+1 don't rely solely on JS to deal with stuff that will reach the DB.
jfrobishow
A: 

You could store the current value in hidden fields.

like so:

<input type="text" id="firstname" value="#firstname#">
<input type="hidden" id="old_firstname"  value="#firstname#">

on post compare each to send the flag to CF.

jfrobishow
Thank you so much! I'm going to use this technique with a MachII filter to loop through and verify if the field is unique :)
Alex
I would advise against storing things in the form scope that will determine validation on the DB side. Remember, I can, for example, use a FireFox add-on to make hidden fields visible/editable. That's why I suggested a persistent scope.
Ben Doom
As Ben Doom pointed out in his comment and his post do NOT trust user input as if it was sane data. It can be easily tampered with. If this is client side validation just to display errors to the user in a pretty format to the user it's fine, if it's your last line of defense before hitting the DB don't use this technique.
jfrobishow
+1  A: 

You could assign an event handler for the onchange even.

For example:

var wasChanged = {};

inputElement.onchange = function () { wasChanged[this.id] = true; }

function validateElement (element) {
   if (wasChanged[element.id]) {
      // validate
   }
}
CD Sanchez
The problem with this approach is that if the user changes the value, it gets flagged. Then if the user decides to change it back to the original value, it still remains flagged.
Gert G