I have a form that's using an anchor for a submit button like this:
<!DOCTYPE html>
<head>
<script src="jquery.js"></script>
</head>
<body>
<form action="" method="post">
<input type="text" pattern="[A-Za-z]{3}" />
<a href="#" class="submit">Submit</a>
<input type="submit" />
</form>
<script>
$(".submit").click( function(e) {
var form = $(this).parents('form:first');
form.submit();
//e.preventDefault();
});
</script>
</body>
</html>
When I click the submit button, I see a message telling me that the validation failed. When I click the submit anchor, the form gets submitted. How can I trigger the validation when using an anchor as a submit button?
I can do this:
<form action="" method="post">
<input type="text" pattern="[A-Za-z]{3}" />
<a href="#" class="submit">Submit</a>
<input style="display: none" id="i_submit" type="submit" />
</form>
//snip
<script>
$(".submit").click( function(e) {
$("#i_submit").click();
});
</script>
And it does validate the form, prevents submission and draws a red border around the input field but it doesn't show any kind of error message like it does when a real submit button is clicked.
How do I get the error message to pop up as if the real submit button was clicked?