I made a form validation script in JS/JQuery for my project, but I have an issue when I want to submit the filled form for a second time (when I click Submit button second time), but also every even time (click Submit 4th, 6th, 8th time...). When I click 1st, 3rd, 5th... time, it normally validates and without error.
I tried to change error variables (using true/false, 1/0, "yes"/"no"), if/else statements, but nothing seems to work. Every time I get the same results.
$(document).ready(function() {
var newHieroglyphCodeRegex = /^((?!Aa\d)(?:[A-Z][a-z]{1,3})[1-9][0-9]{0,2}[a-zA-Z]?)$/g;
$("#js-new-hieroglyph-code-form").submit(function(e){
e.preventDefault();
var inputNewHieroglyphCodeValue = $("#js-new-hieroglyph-code-input").val().trim();
console.log(inputNewHieroglyphCodeValue);
var newHieroglyphCodeInputNumberError;
var newHieroglyphCodeInputFormatError;
var newHieroglyphCodeInputError;
if (inputNewHieroglyphCodeValue.length >= 3) {
newHieroglyphCodeInputNumberError = "no";
} else {
newHieroglyphCodeInputNumberError = "yes";
}
if (newHieroglyphCodeRegex.test(inputNewHieroglyphCodeValue) === true) {
var newHieroglyphCodeInputFormatError = "no";
} else {
var newHieroglyphCodeInputFormatError = "yes";
}
if (newHieroglyphCodeInputNumberError == "no" && newHieroglyphCodeInputFormatError == "no") {
newHieroglyphCodeInputError = "no";
} else {
newHieroglyphCodeInputError = "yes";
}
if (newHieroglyphCodeInputError == "yes") {
if (newHieroglyphCodeInputNumberError == "yes") {
$("#js-new-hieroglyph-code-input").css("border-color", "rgb(190,115,110)");
$("#js-new-hieroglyph-code-input-error-div").css("display", "flex");
var errorMessage = "<div class='error-message error-pointer'><p>Code must have at least 3 characters.</p></div>";
$("#js-new-hieroglyph-code-input-error-div").html(errorMessage);
} else {
$("#js-new-hieroglyph-code-input").css("border-color", "rgb(190,115,110)");
$("#js-new-hieroglyph-code-input-error-div").css("display", "flex");
var errorMessage = "<div class='error-message error-pointer'><p>Code has invalid formatting or characters.</p></div>";
$("#js-new-hieroglyph-code-input-error-div").html(errorMessage);
}
} else {
var errorMessage = "<div class='success-message'><p>Looking good!</p></div>";
$("#js-new-hieroglyph-code-input-error-div").html(errorMessage);
$("#js-new-hieroglyph-code-input-error-div").css("display", "none");
$("#js-new-hieroglyph-code-input").css("border-color", "rgb(95,160,95)");
$.ajax({
url : "./includes/individual_insert_scripts/insert_new_hieroglyph_code.script.php",
method : "POST",
data : {
inputNewHieroglyphCodeValue : inputNewHieroglyphCodeValue,
submit : 1
},
success : function(data) {
$("#js-new-hieroglyph-code-success-message-wrapper").html(data);
}
});
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="js-new-hieroglyph-code-form" class="form-content-holder" method="POST" action="" autocomplete="off" novalidate accept-charset="UTF-8">
<!-- NEW HIEROGLYPH CODE -->
<div class="form-section">
<div class="basic-form-element-container-narrow">
<!-- Label for form field -->
<label for="js-new-hieroglyph-code-input">New hieroglyph code</label>
<div class="form-input-info-wrapper">
<div class="form-input-wrapper">
<!-- Form input field -->
<input id="js-new-hieroglyph-code-input" name="new-hieroglyph-code-input" type="text">
<!-- DIV for error message -->
<div id="js-new-hieroglyph-code-input-error-div" class="validation-message-div">
<!-- Wrong formatting. -->
<!-- Code already exists. -->
<!-- Looking good! -->
</div>
</div>
</div>
</div>
</div>
<!-- SUCCESS MESSAGE -->
<div id="js-new-hieroglyph-code-success-message-wrapper" class="insert-success-message-wrapper">
<!-- You successfully added new hieroglyph code. Proceed to this <a href="./new_hieroglyph.php">page</a> to add new hieroglyph to the database. -->
</div>
<!-- SUBMIT BUTTON -->
<div class="form-submit-button-wrapper">
<button id="js-new-hieroglyph-code-submit-button" name="new-hieroglyph-code-submit-button" class="default-button" type="submit">Submit!</button>
</div>
</form>
Here are the errors: