Sahil
Sahil

Reputation: 1983

function does not always return a value?

I am trying to make this ajax request function work but netbeans is giving a warning that the following function does not always return a value. Can anyone please help.

function fpform(){
    var response='';
    var fpemail = $('#frgtpwd').val();
    //var fpemail = document.getElementById('frgtpwd').value;

    if (fpemail == ""){
        $('span#fperror').text("insert your emal address");
        //document.getElementById('fperror').innerHTML = "Insert your email address";
        return false;
    } else {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(fpemail)==false) { 
            $('span#fperror').text("Email address is not in valid format");
            //document.getElementById('fperror').innerHTML = "Email address is not in valid format";
            return false;
        } else {
            $("#loader").html('<img src="images/ajax-loader.gif" />');
            $.post("forgot_password_process.php", {
                email:fpemail
            }, function(response){
                response = response.trim();
            }).success(function () {
                if (response == 'yes'){
                    $("#fperror").html('<font color="green"><b>Your password has been reset now and emailed to you </b></font>');
                    $("#loader").hide('<img src="images/ajax-loader.gif" />');
                    return true;
                } else {
                    alert("your email address was not found");
                    $("#loader").hide('<img src="images/ajax-loader.gif" />');
                    $("#fperror").html('<font color="black"><b> Email address was not found in database!</b></font>');
                    return false;
                } 
            });
        }
    }
}

Upvotes: -1

Views: 1425

Answers (2)

FLCL
FLCL

Reputation: 2514

You should return value after $.post(...).success(...);

Upvotes: 0

Eric
Eric

Reputation: 97661

The return true; statement in your code is not returning from fpform. It is instead returning from the callback function given to .success(). By the time this function is executed, the outer function, fpform, has long since returned. The only way to "return" from a function using ajax is with a callback.


Before I give you any code, you've made a bunch of other mistakes:

  1. Your email regex, /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, fails on my email address. + is a valid character as well. Consider not validating email addresses with regex.

  2. $("#loader").hide('<img src="images/ajax-loader.gif" />') does not work. At all. You want $("#loader").empty()

  3. The variable response you declare at the top is shadowed by your argument response in one of your anonymous functions, making response = response.trim() have no effect whatsoever.


function fpform(callback) {
    var fpemail = $('#frgtpwd').val();

    if (fpemail == ""){
        $('span#fperror').text("insert your email address");
        callback(false);
    } else {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(fpemail)==false) { 
            $('span#fperror').text("Email address is not in valid format");
            callback(false);
        } else {
            $("#loader").html('<img src="images/ajax-loader.gif" />');
            $.post("forgot_password_process.php", {
                email:fpemail
            }).success(function(response) {
                response = response.trim();
                if (response == 'yes'){
                    $("#fperror").html('<font color="green"><b>Your password has been reset now and emailed to you </b></font>');
                    $("#loader").hide('<img src="images/ajax-loader.gif" />');
                    callback(true);
                } else {
                    alert("your email address was not found");
                    $("#loader").hide('<img src="images/ajax-loader.gif" />');
                    $("#fperror").html('<font color="black"><b> Email address was not found in database!</b></font>');
                    callback(false);
                } 
            }).error(function() { callback(false); });
        }
    }
}

Upvotes: 1

Related Questions