Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1221 How can I validate a post code/zip code with an itermidiate page that states whether it is valid or not?

You are here: irt.org | FAQ | JavaScript | Misc | Q1221 [ previous next ]

The biggest problem you have is the volume of data - it would be a lot better to do this server side.

The second biggest is that JavaScript is not supported or disabled. The following skips the validation process if JavaScript is not present:

<script language="JavaScript"><!--
var p = new Array();

var n = 0;

p[n++] = 'ABC 123';
p[n++] = 'DEF 456';
p[n++] = 'GHI 789';
p[n++] = 'IJK 123';
p[n++] = 'LMN 456';
p[n++] = 'OPQ 789';
p[n++] = 'RST 123';
p[n++] = 'UVW 456';
p[n++] = 'XYZ 789';

function isValidPostCode(postcode) {
    if (postcode == '') {
        alert('Missing postcode');
        return false;
    }

    for (var i=0; i<n; i++) {
        if (postcode == p[i]) {
        location.href='success.htm?postcode=' + escape(postcode);
            return false;
        }
    }

    location.href='reject.htm?postcode=' + escape(postcode);
    return false;
}
//--></script>

<form name="myForm" action="register.htm" method="get" onsubmit="return isValidPostCode(document.myForm.postcode.value.toUpperCase())">
<input type="text" name="postcode">
<input type="submit">
</form>

Then in success.htm and reject.htm:

<b>Postcode [not] valid</b> <!-- adapt per success.htm reject.htm file -->

<form name="myForm" action="register.htm" method="get">
<input type="hidden" name="postcode">
<input type="submit" value="I accept">
</form>

<script language="JavaScript"><!--
function replace(string,text,by) {
    // Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);

    if ((!i) && (text != string.substring(0,txtLength))) return string;
        if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr +=
replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

if (location.search.length > 0) {
    var postcode = unescape(location.search.substring(10));
    document.myForm.postcode.value = replace(postcode,'+',' ');
}
//--></script>

And then in register:

<form name="myForm" action="register.htm" method="get">
<input type="text" name="postcode">
</form>

<script language="JavaScript"><!--
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);

    if ((!i) && (text != string.substring(0,txtLength))) return string;
        if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

if (location.search.length > 0) {
    var postcode = unescape(location.search.substring(10));
    document.myForm.postcode.value = replace(postcode,'+',' ');
}
//--></script>

Kathy Williams writes:

Validating postcodes - Check out www.addresswizard.com they do it all for you and integration only takes minutes.

Feedback on 'Q1221 How can I validate a post code/zip code with an itermidiate page that states whether it is valid or not?'

©2018 Martin Webb