Post Code Validation in PHP

May 18, 2009 at 19:42:38
Specs: Win Vista, P4 3ghz/1gb ram
Hey Guys,

I've got a form that currently validates whether the postcode contains any data at all... if no data found it spits out an error. What I need to do is edit it so that it will actually validate the post code to ensure it is a proper (australian) post code of 4 digits (e.g. 3350) as well as just determining if data has been entered.

This is the code I have so far:

alert('Please enter Shipping Postal/Zipcode');


return false;


Thanks in advance guys :)

See More: Post Code Validation in PHP

Report •

May 29, 2009 at 13:33:39
I see that this is sort of an old post, but maybe I can still help out...

If all you are trying to do is make sure that 'sZipCode' is 4 numerical digits, then you can use a regular expression. Try something like this:

if( !ereg( "[0-9]{4}", $('sZipcode').value) )
echo "Please enter a valid Shipping Postal/Zipcode";

Basically, the ereg() function compares the second argument with the regular expression supplied as the first argument and if they match, returns true, otherwise returns false. If you do not know what regular expressions are I can try to help you out...

There is perhaps a better way of doing this using PEAR. I cannot really give you any details about PEAR, because I am sort of a novice php programmer, but PEAR is sort of an extension for php that includes packages of code that others have written that can be very helpful, especially in validating forms. There is a Validate_AU package available for PEAR that would be able to validate the zip code to determine that it is a valid australian post code... the code I showed you before will only determine that the value is any four digits, so anything from 0000 - 9999 would be accepted.

Definitely feel free to ask for any further explanation about anything. I'll try my best to help out, but like I said I'm just a novice at php.

And... maybe you found your answer already :)

Report •
Related Solutions

Ask Question