OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Custom function to check blank inputs

  • Thread starter Thread starter John Beasley
  • Start date Start date
J

John Beasley

Guest
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:

Code:
$("#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:

Code:
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:

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

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

Code:
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" into the checkFormErrors function like so:

Code:
checkFormErrors(addobj.addReference, "#addReferenceError", 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?

<p>Trying to build a custom function that checks for errors and then shows the error.</p>
<p>As of right now, the whole process looks like this:</p>
<pre><code>$("#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
}
});
</code></pre>
<p>So a reusable function would help me out here.</p>
<p>So I started to build a reusable function outside of the on click function above. It currently looks like this:</p>
<pre><code>function checkFormErrors(obj, errorMessage) {
if(obj == "") {
$(errorMessage).show();
errors = errors + 1;
}
return errors;
}
</code></pre>
<p>Then inside my on click function, I am calling the checkFormErrors function like this:</p>
<pre><code>checkFormErrors(addobj.addReference, "#addReferenceError");
</code></pre>
<p>Upon submitting the form, I am getting the console error:</p>
<pre><code>ReferenceError: errors is not defined
</code></pre>
<p>What did I do wrong, and how can I make this function work properly?</p>
<p>*** EDIT ***</p>
<p>So I'm passing "errors" into the checkFormErrors function like so:</p>
<pre><code>checkFormErrors(addobj.addReference, "#addReferenceError", errors);
</code></pre>
<p>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.</p>
<p>How can I fix this?</p>
 
Top