Forum Moderators: open
function testPostCode () {
var myPostCode = document.getElementById('postcode').value;
if (checkPostCode (myPostCode)) {
document.getElementById('postcode').value = checkPostCode (myPostCode)
document.pracsearch.submit()
}
else {alert ("Postcode has invalid format")};
}
HTML:
<form action="..." onsubmit="return testPostCode();">
:
<input type="submit" name="btn_submit" value="ok">
</form>
JavaScript:
function testPostCode() {
var myPostCode = document.getElementById('postcode').value;
if (checkPostCode(myPostCode)) {
document.getElementById('postcode').value = checkPostCode(myPostCode);
return true;
} else {
alert("Postcode has invalid format");
return false;
}
} (I did just correct the positioning of your semicolons (;) a tad)
If you were to solely rely on JS to submit your form (ie. not actually having a submit button) then it would only get submitted if JS was available, at least this way it is only your client-side validation which is compromised.