I'm trying to bind some manual validation to the on('submit')
event in jQuery after pausing the submission, but have it still perform the browser's native HTML validation. However, I cannot get it to work.
This is my bare-bones code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Prevent Form Submission</title>
</head>
<body>
<form>
<input type="text" name="example" required>
<input type="submit">
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).on('click', 'input[type=submit]', function(e) {
e.preventDefault();
// Do something here
$('form').submit();
});
</script>
</body>
</html>
With this code, I can prevent the form from submitting, but then when I run the submit()
event it "skips" the HTML validation and just submits, even if the required field is empty. I am using Google Chrome Version 67.0.3396.99 (Official Build) (64-bit).
If I change the jQuery to this:
$(document).on('submit', 'form', function(e) {
e.preventDefault();
// Do something here
$(this).submit();
});
I cannot even get it to submit.