0
$(document).ready(function() {
    $("form").submit(function(e) {

         if (!checkStockOfProduct())
         {
           e.preventDefault();
           return false;
         }
         return true
    });
});

<script src="~/Scripts/storeinputoutput.js"></script>

storeinputoutput.js file:

function checkStockOfProduct() {
    var allowSubmitForm = true;
    $("table#StoreItemdatatable > tbody > tr").each(function () {

    .
    .
    .

        var storeId = $('#StoreID').val();

        var stock = null;
        var URL = '/aaa/bbb' 
        $.ajax({
            async: false,
            url: URL,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            data: {
                productNServiceID: productNServiceID,
                storeID: storeId
            },
            type: 'POST',
            success: function (data) {
                stock = data.Stock;
            }
            , error: function (jqXHR, exception) {

            }

        });



        if (...) {
            allowSubmitForm = false;
        }
        return allowSubmitForm;

    });
}

In form submit , I called the function and I wouldn't do the submit operation if function return false. But the value of the xx variable is always undefined.

Please advise

Checked using Alert. variable "allowSubmitForm" in function "checkStockOfProduct" in the last line has value and not "undefined" But in Submit it is undefined

Miss
  • 171
  • 1
  • 4
  • 10
  • 8
    false and 'false' are not the same. – ThisIsNoZaku Feb 12 '20 at 17:44
  • 3
    If you want to return a boolean, don't put quotes around the value. That's a string. – Barmar Feb 12 '20 at 17:44
  • 1
    @Barmar @ThisIsNoZaku he says it's returning `undefined`. If the reason whas the string 'false' and 'true', he would be receiving a string, and having another kind of bug. We need more code @Miss, so we can understand why it's not working – CH4B Feb 12 '20 at 17:52
  • 3
    I'm worried the `checkStockOfProduct` function may be overly simplified from what's really going on. It has a rogue `});` in it that I would guess might be an async call. With or without quotes, the code you've shared shouldn't return `undefined` – Corey Ogburn Feb 12 '20 at 17:58
  • That said, if you only ever expect to get values of `true` or `false` and don't expect any other values, why are you bothering to write `if (xx === false)` as your condition? It seems cleaner to write as `if (!xx)`. The performance improvement of using `===` isn't big enough to care about in this situation. – JMoravitz Feb 12 '20 at 18:06
  • The problem is that the function checkStockOfProduct always returns undefined. – Miss Feb 12 '20 at 18:56
  • 1
    @Miss Given the snippet you've provided, that simply isn't possible. I'm going to agree with the previous comments and guess that you may have over-simplified your code to such an extent that the cause has been removed. I'll also agree with Corey's hunch that this relates to ["How do I return the response from an asynchronous call?"](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Tyler Roper Feb 12 '20 at 18:58
  • There is probably an error in your code for `checkStockOfProduct()` which is causing an exception, making it so that you don't successfully make it to a return statement. – JMoravitz Feb 12 '20 at 19:01
  • Checked using Alert. variable "allowSubmitForm" in function "checkStockOfProduct" in the last line has value and not "undefined" But in Submit it is undefined – Miss Feb 12 '20 at 19:29
  • 2
    If you want help with your question, you're going to have to share your **actual code**. We can't just keep guessing at the issue when you've shared a completely working snippet. – Tyler Roper Feb 12 '20 at 19:29
  • What do you mean by 'Submit is undefined'? – Saeed D. Feb 12 '20 at 19:50
  • The reason why is simple, as explained in my answer. You are getting undefined because you aren't making any conditional checks if your function returns a true value. Even though your checkStockOfProduct is incorrectly returning either "true" or "false, these strings are considered TRUE. Therefore, you always get undefined because you don't consider what happens if a true value is returned. – alex067 Feb 12 '20 at 20:28
  • @TylerRoper : i share actual code... – Miss Feb 14 '20 at 11:56
  • @Miss You have `if (...)` in your actual code? – Tyler Roper Feb 14 '20 at 15:14

2 Answers2

0
  1. I understand you want to return boolean, so use true and false rather than 'true' and 'false' Why? The ones without quotes are boolean while the ones with quotes are string. A non-empty string in javascript would always return true while an empty string would return false (youare returnimg non-empty strings there and the result is obviously not what you would want in this case).

  2. var is a little tricky and I would suggest we use let next time. Since it doesnt seem as though you are doing a lot in the checkStockOfProduct function I would refactor it to ==>

const checkSumOfProduct = (whatever...) ? false : true
  1. I noticed the update on your question, I really don't get what you are trying to do there but to me it looks as though the result of the checkStockOfProduct decides whether you would prevent default form submission on submit and return false or not. I would also be assuming that the CheckSumOfProduct function is synchronous Let's refactor that code to do the same thing you were trying to do but look something like this ==>
$(document).ready(function() {
    $("form").submit(function(e) {
        if(!CheckStockOfProduct()){
           e.preventDefault()
           return false
         } 
           return true
    });
});

Let me know if this works out...I am in transit and on mobile atm

Tolumide
  • 944
  • 9
  • 11
-1

Try

function checkStockOfProduct() {
    var allowSubmitForm = true;
    if (...) {

        allowSubmitForm = false;
    }

    return allowSubmitForm;

  };

And adjust your submit() event handler based on the function return value:

$(document).ready(function () {
    $("form").submit(function (e) {
       if (checkStockOfProduct()) {
          //valid
          return;
       }
       //invalid
       e.preventDefault();
    });
});
Saeed D.
  • 1,125
  • 1
  • 11
  • 23
  • Why bother with `if (!Boolean(xx))` rather than `if (!xx)`? What benefit does using the Boolean object type have here compared to more traditional booleans? – JMoravitz Feb 12 '20 at 18:11
  • @JMoravitz You can use the traditional !xx but the advantage of Boolean() function is that it will always return true or false regardless of variable type. For example, if xx is 'undefined' or 'null' the comparison of 'if (xx==false)' will return false but 'if (Boolean(xx)==false)' will return true as expected. – Saeed D. Feb 12 '20 at 18:34
  • 1
    And by completely removing the unnecessary `== false` part of the condition, you have `(!xx)` will return true for all falsy values including false, undefined, null, and NaN etc..., regardless whether or not `(xx == false)` would return true or not. Further, since we had control over `xx`, we should be able to guarantee what value it could have at the time that we check with it. – JMoravitz Feb 12 '20 at 18:38
  • The problem is that the function checkStockOfProduct always returns undefined. – Miss Feb 12 '20 at 18:56
  • @SaeedD. Again., don't use `== false`. Remove that completely. You will have `if (Boolean(xx)==false)` and `if (!xx)` will both return true. `if (xx == false)` will return false, but that is neither here nor there. – JMoravitz Feb 12 '20 at 18:59
  • 1
    @Miss You have an extra `)` in the last line of the code in checkStockOfProduct(). – Saeed D. Feb 12 '20 at 19:02
  • 1
    This is helpful but doesn't answer OP's question, which is *"Why does this return `undefined`?"*. These pointers would be more appropriate as comments (which other users have already made). – Tyler Roper Feb 12 '20 at 19:22
  • Checked using Alert. variable "allowSubmitForm" in function "checkStockOfProduct" in the last line has value and not "undefined" But in Submit it is undefined – Miss Feb 12 '20 at 19:29
  • @TylerRoper The function does not return `undefined` once the syntax is fixed and proper comparison is made. I tested the initial code and the fix in jsfiddle. – Saeed D. Feb 12 '20 at 19:30
  • The function does not return `undefined` at *any point*, with or without the syntax error. The question being un-answerable in its current state doesn't justify answering it. – Tyler Roper Feb 12 '20 at 19:32
  • @Miss then your problem is not with xx. You want to allow submit if the function returns true and prevent it if it is false. I made corrections to the code. Try it to see if it solves your problem. – Saeed D. Feb 12 '20 at 19:46