I'm not that experienced with JavaScript and I’ve been trying for a while now to make this form display an alert message if a postcode is entered in the wrong format when the user clicks the submit button. What I’ve achieved so far I’ve placed all inside one function which validates all of the fields, I’m using names to make reference to the fields from inside the function. (Sorry if I haven’t indented properly)
var g=document.forms["myform"]["postcode"].value;
if (g==null || g=="")
{
alert("Please enter your Post CODE");
return false;
}
var regPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9]
[a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;
if(regPostcode.test(postcode) == false)
{
alert("That Post Code is incorrect");
return false;
}
Here's the HTML for the particular field inside the form im using
<div class=”field_container”><label>Post Code</label><input type=”text” name="postcode" id="postcode" /></div>