The easy answer is "no, it doesn't work like that." I assume you mean
you want validateField to return based on the result of the $.get
callback? The whole point of the callback stuff is that the validate
function has long since finished executing by the time your code in
the callback function starts executing, which means there's no chance
for returning from validateField via your callback.

The "right" way to deal with this (assuming you believe your design is
sound) would be to pass a callback to validateField. So instead of:

if(validateField(...)) {
 // success stuff
} else {
 // failure stuff
}

You would do:

validateField(..., function(succeeded) {
 if(succeeded) {
   // success stuff
 } else {
   // failure stuff
 }
});

And change validateField to look something like this:

function validateField(file, field, criteria, callback){
   $("#"+field+" img.ico").attr("src","/images/office/ico-loading.gif");
   $.get("/gateway/validate-"+file+".cfm",
       {field: field, value: $("#"+field+" input").val(), criteria: criteria},
       function(response) {
           response = eval("("+response+")");
           showIcon(field,response.pass,response.msg);
           if(callback) {
               callback(/* true or false */);
           }
       }
   );
}

But again, you may want to rethink your design, or maybe consider
using the jQuery validation plugin:
http://jquery.bassistance.de/validate/demo-test/

--Erik

On 5/15/07, Paul Malan <[EMAIL PROTECTED]> wrote:

Hi all, I'm sorry if this has been answered before; it seems simpler
than I'm making it.

I am validating a form field on blur by calling a function
validateField.  This function handles the ajax interaction and passes
the response to another function showIcon, which determines which icon
to display and may or may not display an error message div.

This is all working, but now I want to display only the first form
field on load.  The remainder of the form should display only if the
first field passes muster, so I'd simply like to have my validateField
function return true or false.  What could be simpler?  (a lot,
apparently!)

Here is the function.  Is there an easy way to have it return true/
false?  I don't want to interrupt the normal callback handoff to
showIcon, I just want the calling page to have access to the result.

function validateField(file, field, criteria){
                $("#"+field+" img.ico").attr("src","/images/office/ico-
loading.gif");
                $.get("/gateway/validate-"+file+".cfm",
                                {field: field, value: $("#"+field+" 
input").val(), criteria:
criteria},
                                function(response){
                                        response = eval("("+response+")");
                                        
showIcon(field,response.pass,response.msg);
                        });
        }


Reply via email to