I am validating a form with JS. The validation is supposed to make error messages appear after elements if the name, etc. aren't filled in.
var contactModal = '<p class="name">Name: <input type="text" autofocus name="name" id="name" class="form-control" id="fd"> <span class="error"></span></p>
<p class="email">Email: <input type="email" name="email" id="email" class="form-control"><span class="error"></span><br> <div class="input-group"><span class="input-group-addon"><input type="checkbox" aria-label="..." name="copy"></span> <input type="text" readonly disabled class="form-control" aria-label="" value="Check here for a copy of your message." style="border:1px solid #ccc !important;cursor:default !important;"></div></p>
<p class="msg">Message: <div class="textarea form-control" id="msg" name="msg" contentEditable style="height:175px;"></div> <span class="error"></span></p></div>';
$("body").append(contactModal);
$("a#contact").click(function(e){ e.preventDefault(); $(".cont").modal(); });
// Handle contact form submission
$(".cont form").submit(function(e){
var name = $("#name").val();
var email = $("#email").val();
var msg = $("#msg").html();
var errorMsg;
if(name == "" || email == "" || msg == ""){
e.stopPropagation();
e.preventDefault();
if(name == ""){
$(".name .error").html("Please enter a name.");
}
if(email == ""){
$(".email .error").html("Please enter an email address.");
}
if(msg == ""){
$(".msg .error").html("Please write a message.");
} else {
$(".cont").modal('hide');
}
}
});
Line breaks added for readability.
The first two - the name and the email - get the error messages if they aren't filled in, but the message one doesn't. Why isn't that one working, and how can I make it work.
UPDATE:
The form is not submitting, so the error is there, but the message is not appearing.
tag cannot contain block-level elements (div). browser will end
tag before the