The same way you put it there, right next to your alert
:
if (errorMessage==""){
$("#error").html("");
alert("Your message has been successfully sent!");} else {
$("#error").html(errorMessage);}
or simply
$("#error").html(errorMessage);
if (errorMessage==""){
alert("Your message has been successfully sent!");}
...since errorMessage
has ""
in it already.
Example:
Here's your original:
$("#validationForm").submit(function(event) {
var errorMessage = "";
event.preventDefault();
function isValidEmailAddress(emailAddress) {
var pattern = new
RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};
/* Regex validation of email address */
if (!isValidEmailAddress($("#email").val())) {
errorMessage = "*Please enter a valid email address*";
}
if (errorMessage == "") {
alert("Your message has been successfully sent!");
} else {
$("#error").html(errorMessage);
}
});
<p>Your original code</p>
<form id="validationForm">
<div><input type="text" id="email"></div>
<div id="error"></div>
<input type="submit" value="Send">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Here's the updated version:
$("#validationForm").submit(function(event) {
var errorMessage = "";
event.preventDefault();
function isValidEmailAddress(emailAddress) {
var pattern = new
RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};
/* Regex validation of email address */
if (!isValidEmailAddress($("#email").val())) {
errorMessage = "*Please enter a valid email address*";
}
$("#error").html(errorMessage);
if (errorMessage == "") {
alert("Your message has been successfully sent!");
}
});
<p>Your original code</p>
<form id="validationForm">
<div><input type="text" id="email"></div>
<div id="error"></div>
<input type="submit" value="Send">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
It's purely a style thing, but can I strongly urge you to use any of the several vaguely-common bracing and indentation styles instead of hiding the ending }
on the last line of the block and not indenting when you've made something conditional? That style is incredibly error-prone.
Here's the simple version above in the most common bracing and indentation style:
$("#error").html(errorMessage);
if (errorMessage=="") {
alert("Your message has been successfully sent!");
}
Here's the longer version with the else
in that same style::
if (errorMessage=="") {
$("#error").html("");
alert("Your message has been successfully sent!");
} else {
$("#error").html(errorMessage);
}
or sometimes you see else
starting a new line:
if (errorMessage=="") {
$("#error").html("");
alert("Your message has been successfully sent!");
}
else {
$("#error").html(errorMessage);
}
or another common (though much less common) one:
if (errorMessage=="")
{
$("#error").html("");
alert("Your message has been successfully sent!");
}
else
{
$("#error").html(errorMessage);
}
Similarly, spaces around binary and ternary operators aid readability:
if (errorMessage == "")
// ^ ^