Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 455
Ken Avatar asked Sep 14 '12 13:09

Ken


People also ask

How check jQuery true or false?

To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);

Which function returns true if the expression?

value_if_true - The value the function returns if logical_expression is TRUE .

How do you know if a function is a condition?

Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it's false. For example: =IF(A2>B2,"Over Budget","OK")


4 Answers

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) {
...
}
like image 66
Christian Avatar answered Oct 22 '22 20:10

Christian


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'.

like image 43
JDandChips Avatar answered Oct 22 '22 21:10

JDandChips


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?

like image 24
ferry Avatar answered Oct 22 '22 21:10

ferry


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) { ...
like image 1
lc. Avatar answered Oct 22 '22 20:10

lc.