You can use jQuery like this:
$(function() {
$("#form").submit(function(event) {
// do some validation, for example:
username = $("#username").val();
if (username.length >= 8)
return; // valid
event.preventDefault(); // invalidates the form
});
});
In your HTML:
<form id="form" method="post">
<input type="text" name="username" required id="username">
<button type="submit">Submit</button>
</form>
References:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event https://api.jquery.com/submit/