check to see if function has been run?

check to see if function has been run?

Hi Folks,

I've built a script that checks to see if a form input has a value, and if not, write an error with the field's label name in it. If they go back and fill in the field, it removes the error. It works great, but there's one issue; every time the user focuses out of the field, it writes the error again. I need to make it so that it only writes the error once. Using .one doesn't seem to work. Here's the html:

<label for="name">Name</label>
<input id="name" type="text" required>
<label for="email">Email</label>
<input id="email" type="email" required>

and the js:

$('input[required]').focusout(function() {
  var labelvalue = $('label[for=' + $(this).attr('id') + ']').html();

  if (!$(this).val()) {
    $(this).after('<p class="error" role="alert">The ' + labelvalue + ' field is required</p>');
  } else {
    $(this).next('.error').remove();
  }
});

Here's a fiddle as well: