0

Trying to build a custom function that checks for errors and then shows the error.

As of right now, the whole process looks like this:

$("#addRecordSubmit").on("click", function(e) {
  e.preventDefault();
  let errors = "";

  let addobj = {
    addReference: $("#addReference").val(),
    addAccount: $("#addAccount").val(),
    addDescription: $("#addDescription").val(),
    // some more items
  };

  if (addobj.addReference == "") {
    $("#addReferenceError").show();
    errors = errors + 1;
  }
  if (addobj.addAccount == "") {
    $("#addAccountError").show();
    errors = errors + 1;
  }
  if (addobj.addDescription == "") {
    $("#addDescriptionError").show();
    errors = errors + 1;
  }
  // some more if statements

  if (errors > 0) {
    return false;
  } else {
    // process submit
  }
});

So a reusable function would help me out here.

So I started to build a reusable function outside of the on click function above. It currently looks like this:

function checkFormErrors(obj, errorMessage) {
  if(obj == "") {
    $(errorMessage).show();
    errors = errors + 1;
  }
  return errors;
}

Then inside my on click function, I am calling the checkFormErrors function like this:

checkFormErrors(addobj.addReference, "#addReferenceError");

Upon submitting the form, I am getting the console error:

ReferenceError: errors is not defined

What did I do wrong, and how can I make this function work properly?

*** EDIT ***

So I'm passing "errors" from the on click into the checkFormErrors function like so:

$("#addRecordSubmit").on("click", function(e) {
  e.preventDefault();
  let errors = "";

  let addobj = {
    addReference: $("#addReference").val(),
    addAccount: $("#addAccount").val(),
    addDescription: $("#addDescription").val(),
    // some more items
  }

  checkFormErrors(addobj.addReference, "#addReferenceError", errors);

  if (errors > 0) {
    return false;
  } else {
    // process submit
  }
});

So the checkFormErrors function now looks like this:

function checkFormErrors(obj, errorMessage, errors) {
  if(obj == "") {
    $(errorMessage).show();
    errors = errors + 1;
  }
  return errors;
}

I'm no longer getting the "error is not defined" message, but I am not getting the error count returned back into the on click function.

How can I fix this?

9
  • let errors = ""; sets the scope inside the onclick function, but you're trying to reference it from checkFormErrors(); you may need to set it as a global variable or pass it to checkFormErrors()
    – mykaf
    Commented Jun 25 at 16:25
  • @mykaf - I updated the question to include your suggestion. Commented Jun 25 at 16:37
  • Your click listener doesn't seem to call checkFormErrors(). Where are you expecting to see this ? Also, please edit your code here to reflect changes made.
    – mykaf
    Commented Jun 25 at 16:39
  • @mykaf - I did update the code underneath where it reads EDIT. I am trying to return the count of errors into the on click function. I hope that makes sense. Commented Jun 25 at 16:42
  • 1
    Not a bro, but thanks.
    – mykaf
    Commented Jun 25 at 17:35

1 Answer 1

1

There seem to be be a few things going on here:

  1. let creates a block-scoped variable, so it can only be used within the block it's declared it. You would need to create a global variable instead or pass the variable to checkFormErrors().
  2. checkFormErrors() returns the error count, but you're not saving the result to use in your click listener. Update errors with errors = checkFormErrors(...);
1
  • Thanks again. I'm encountering another problem, but I'll have to create another question for it. Commented Jun 25 at 17:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.