0

I am attempting to pass a variable into a function when a form submit button is clicked:

function validateMyTransfer()
{

    var accounttype = $("#transfer_from option:selected").attr('accountdescription');
    if (accounttype == "Current Account")
    {
        var sharebalance = $("#transfer_from option:selected").attr('balance');

        if (newloanbalance > sharebalance)
        {
            alert("You cannot transfer an amount greater than that which is owed in loans from a Share Account.")
            return false
        }
    }
    return true
}

The above function, situated before document.ready, checks if the selected account is a current account. If it is the balance is checked against a total balance from all loan accounts (this is passed in from the second block of code). If the loan balance is greater than the account balance i wish to return false, stop the form submission and alert the user.

var loanbalance = 0;
$('#transfer_from option').each(function()
{
    var producttypeid = $(this).attr('producttype');
    var balance =  $(this).attr('balance');

    if (balance == 0 || producttypeid == 2)
    {
        loanbalance = parseFloat(loanbalance) + parseFloat(balance);
        $(this).remove();
    }
});

$("#loanbalance").val(loanbalance);
newloanbalance = loanbalance;

$("#transfer-submit").click(validateMyTransfer(newloanbalance));

The above code, situated in the document.ready block, runs through each select option, if its balance is 0 or producttype is 2 (loan product) the balance of this option is added to loanbalance and the option is removed from the select. I then pass this into validateMyTransfer with newloanbalance. The problem is it is not evaluating the function as true or false and still submitting the form. How would i go about correcting this?

user3385136
  • 513
  • 1
  • 7
  • 20

1 Answers1

0

You need to parse the shareBalance :

var sharebalance = parseInt($("#transfer_from option:selected").attr('balance'));

or

var sharebalance = parseFloat($("#transfer_from option:selected").attr('balance'));
Harshit Gupta
  • 719
  • 1
  • 9
  • 26