First off and this may or may not be a problem in some browsers, the use of left and right quotes (you only have left ones or right here):
Code:
<div class=”field_container”><label>Post Code</label><input type=”text”
is generally invalid. They should be " symbols instead.
I'm ignorant of what constitutes a valid UK postal code. However, the regular expression in your post has an obvious logical flaw at the end:
I'm almost certain these were meant to mean all upper and lower case letters like the others:
so that could esily be the problem or at least a part of it.
Also, [0-9] means the same as \d. And letters may be flagged as case insensitive with the i switch at the end of the expression. Additionally ([ ]) means the same as ( ), and the use of {1}, which means one and only one time is the default, so is unnecessary.
With all that in mind, the entire expression can be considerably shortened to*:
Code:
/^([a-z])(\d\d|\d|[a-z]\d[a-z]|[a-z]\d\d|[a-z]\d)( )(\d[a-z][a-z])$/i
Oh, and:
Code:
if(!regPostcode.test(postcode))
is the same as:
Code:
if(regPostcode.test(postcode) == false)
So you would have:
Code:
// Check for revising wherever the inputted data is in the correct format e.g. UK Post Code //NEED HELP WITH THIS ONE
var regPostcode = /^([a-z])(\d\d|\d|[a-z]\d[a-z]|[a-z]\d\d|[a-z]\d)( )(\d[a-z][a-z])$/i;
if(!regPostcode.test(postcode))
{
alert("Your Post Code is in the wrong format");
contactform.postcode.focus();
return false;
}
If that doesn't take care of the problem, let me know - and tell me the criteria for a valid UK postal code. A link to your page would be a great help.
One other thing. regPostcode.test(postcode) doesn't tell us whether or not postcode has been properly defined elsewhere as the value of the postcode input. If it hasn't, then most likely it will always fail the test.
Here's a working demo that includes an example of the shortest and the longest code that would fit the criteria, which are outlined in plain words:
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
// Check for revising wherever the inputted data is in the correct format e.g. UK Post Code //NEED HELP WITH THIS ONE
function cpc(){
var regPostcode = /^([a-z])(\d\d|\d|[a-z]\d[a-z]|[a-z]\d\d|[a-z]\d)( )(\d[a-z][a-z])$/i;
if(!regPostcode.test(document.getElementById('postcode').value))
{
alert("Your Post Code is in the wrong format");
document.getElementById('postcode').focus();
return false;
}
}
</script>
</head>
<body>
<div class="field_container"><label>Post Code</label><input type="text" name="postcode" id="postcode" /></div>
<input type="button" value="Check Code" onclick="cpc();">
<br>shortest valid code: 'a1 1aa'
<br>longest valid code: 'aa1a 1aa'
<div>Criteria</div>
<pre>one letter followed by:
a. one or two numbers
or
b. one letter one number one letter
or
c. one letter and two numbers
or
d. one letter and one number
followed by:
a space one number and two letters.</pre>
</body>
</html>
*a little more shortening is possible.
Bookmarks