I'm not sure what's the best way to do this. The form has a single text input but I'm not actually using the form data for anything.
I just need to validate that something has been entered. If it has, the form will then be submitted and a function called.
At this stage I'm just trying to get a basic example working with a bare minimum of code. I was thinking of something along the lines of the following:
HTML
<!DOCTYPE html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous">
</script>
</head>
<body>
<form name="myForm" onSubmit="return myFunctionName(event)">
<input id="postcode" name="postcode" required>
<input type="submit" value="Click Me">
</form>
</body>
</html>
jQuery
<script>
var postreg = /^\d{6}$/;
var postcode = document.getElementById("postcode").value;
function myFunctionName(e) {
e.preventDefault();
if (postcode.match(postreg)) {
alert('success');
} else {
alert('please enter the correct 6 digit postcode');
}
}
</script>