Ken
Ken

Reputation: 833

How to check the function's return value if true or false

I have a function here that validate fields in a form if they are empty.

function ValidateForm()
{
    jQuery('span.error_msg').hide();
   var success = true;
    jQuery("#shippingF input").each(function()
        {
            if(jQuery(this).val()=="")
            {
                jQuery(this).next().show();
                success = false;
            }
    });
    return success;
}

Now I wanted to use that function here:

function post(url,formId) {
      jQuery("#process").html('<img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/ajax-loader.gif'; ?>" alt="loading" title="ajax-loader" width="16" height="16" class="alignnone size-full wp-image-134">');
     jQuery.post(url, jQuery('#' + formId).serialize(), function(d) {
        jQuery('html,body').animate({scrollTop: jQuery("#scrollhere").offset().top},'slow');
        jQuery("#response").html('<center><p style="height:820px"><span style="color:black;font-weight:bold;font: 11px arial,verdana,sans-serif;"><b>Loading available payment getaways..</b></span><br/><img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/8-1.gif'; ?>" width="220" height="19" /></p></center>');
        jQuery("#response").load("<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/checkout_payment.php'; ?>", function() { Cufon.refresh(); });
        jQuery("#response").attr("style","height:1030px");
    });
}

I tried it and I come up with this.

function post(url,formId) {
 ValidateForm();
 if(ValidateForm() == 'false') {
   jQuery('html,body').animate({scrollTop: jQuery("#shippingF").offset().top},'slow');
 } else {
      jQuery("#process").html('<img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/ajax-loader.gif'; ?>" alt="loading" title="ajax-loader" width="16" height="16" class="alignnone size-full wp-image-134">');
     jQuery.post(url, jQuery('#' + formId).serialize(), function(d) {
        jQuery('html,body').animate({scrollTop: jQuery("#scrollhere").offset().top},'slow');
        jQuery("#response").html('<center><p style="height:820px"><span style="color:black;font-weight:bold;font: 11px arial,verdana,sans-serif;"><b>Loading available payment getaways..</b></span><br/><img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/8-1.gif'; ?>" width="220" height="19" /></p></center>');
        jQuery("#response").load("<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/checkout_payment.php'; ?>", function() { Cufon.refresh(); });
        jQuery("#response").attr("style","height:1030px");
    });
 }
}

The problem is that, the validation is working.. however, .post() function runs even though there is empty field. I guess its on the if/else condition.. is there a better way to accomplish this?

Thank you.

Upvotes: 18

Views: 128517

Answers (5)

JDandChips
JDandChips

Reputation: 10100

You don't need to call ValidateForm() twice, as you are above. You can just do

if(!ValidateForm()){
..
} else ...

I think that will solve the issue as above it looks like your comparing true/false to the string equivalent 'false'.

Upvotes: 12

lc.
lc.

Reputation: 116458

Wrong syntax. You can't compare a Boolean to a string like "false" or "true". In your case, just test it's inverse:

if(!ValidateForm()) { ...

You could test against the constant false, but it's rather ugly and generally frowned upon:

if(ValidateForm() == false) { ...

Upvotes: 1

Christian
Christian

Reputation: 389

false != 'false'

For good measures, put the result of validate into a variable to avoid double validation and use that in the IF statement. Like this:

var result = ValidateForm();
if(result == false) {
...
}

Upvotes: 24

Ricardo Lohmann
Ricardo Lohmann

Reputation: 26320

ValidateForm returns boolean,not a string.
When you do this if(ValidateForm() == 'false'), is the same of if(false == 'false'), which is not true.

function post(url, formId) {
    if(!ValidateForm()) {
        // False
    } else {
        // True
    }
}

Upvotes: 0

ferry
ferry

Reputation: 437

you're comparing the result against a string ('false') not the built-in negative constant (false)

just use

if(ValidateForm() == false) {

or better yet

if(!ValidateForm()) {

also why are you calling validateForm twice?

Upvotes: 6

Related Questions