The following function checks errors in my form. One of the fields must be validated in the server. However, when I post the field the function continue and return from the function before I get answer from the server. I must get the result from the server before returning from the function. This is my code:
function checkErrors()
{
var ErrorMsg = "";
var buyerPublicKey = document.getElementById("BuyerAddressTxt").value;
if (buyerPublicKey == "")
{
ErrorMsg = ErrorMsg+"Please Insert buyer Public Key.<br>";
}
var dealPrice = document.getElementById("PriceEthTxt").value;
if (dealPrice == "")
{
ErrorMsg = ErrorMsg+"Please Insert buyer price.<br>";
}
var timeTobeOpen = document.getElementById("TimeSelector").value;
if (timeTobeOpen == "Deafult")
{
ErrorMsg = ErrorMsg + "Please Insert time.<br>";
}
var url = "/CreateContract/CheckBuyerPublicKeyLegality";
$.post(url, { BuyerPublicKey: buyerPublicKey }, function (data) //Here the problem begins
{
console.log("Buyer balance"+data)
if (data == -1)
{
ErrorMsg = ErrorMsg + "Buyer Public Key is illegal.<br>";//
console.log("1-->" +ErrorMsg );
}
});
console.log("2-->" +ErrorMsg );
return ErrorMsg;
}
As you can see, I get the 2--> ErrorMessage before the 1--> ErrorMessage , which means the the posting operates asynchronously, while I need it to be synchronously.
What can I do here to change it?