Hi

I am using some code to validate uk postcode. I was wondering how I could make this work with AJAX so that the check is carried out on entry. Here is what I have so far:

<form name="postcodeform"  method="post">
   <input type="postcode" /> Postcode<br />
</form>


function IsPostcode($postcode)
{
$postcode = strtoupper(str_replace(' ','',$postcode));
if(preg_match("/^[A-Z]{1,2}[0-9]{2,3}[A-Z]{2}$/",$postcode) || preg_match("/^[A-Z]{1,2}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{2}$/",$postcode) || preg_match("/^GIR0[A-Z]{2}$/",$postcode))
return true;
else
return false;
}


if (IsPostcode($postcode))
print "Valid Post Code";
else
print "Invalid Post Code";

Do you want to call your PHP script with an AJAX call, or do you want to do the check in Javascript?

Sorry didn't make that clear. Do the check in PHP

I tried to carry it out using jquery, but I can't get it to work:

<html>

<head>
<script src="/jquery/jquery.js"></script>
<script src="/jquery/jquery.validate.js"></script>
<script>
$(document).ready(function(){

                                $.validator.addMethod(
                                        "validpostcode",
                                        function(value, element, regexp) {
                                                var check = false;
                                                var re = new RegExp(regexp);
                                                return this.optional(element) || re.test(value);
                                        },
                                        "Please enter a valid postcode."
                                );

    $("#commentForm").validate({

    rules: {
        postcode: {
                required: true,
                minlength: 3,
                validpostcode: "^([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}$"
        },


    messages: {
        postcode: {
                required: "Please Enter Postcode",
                minlength: jQuery.format("You need to use at least {0} characters for your postcode.")
                }
    }

    });
});

</script>
</head>

<body>

<form id="commentForm" method="get" action="">
 <fieldset>
   <p>
     <label for="cpostcode">Postcode</label>
     <em>*</em><input id="cpostcode" name="postcode" size="25" />
   </p>
   <p>
     <input class="submit" type="submit" value="Submit"/>
   </p>
 </fieldset>
 </form>


</body>
</html>

What do you mean by "not working"? Does it mean that it doesn't correctly match or it doesn't display? I'm quite sure that your regex doesn't work in all cases because you are not utilizing regex advantages.

If you are matching postal code that has multiple formats, you should attempt to match each format one-by-one, not try to match all formats in one regex.

The meaning of [a-z]{1} is the same as [a-z] because the latter already implies that there is 1.

// need code block for this to display multiple empty spaces...
The [      ] is the same as [ ]+ or better yet use \s+ which
includes all white space characters.

The [0-9][0-9][0-9] is the same as [0-9]{3} which is more readable.

The ^ and $ only works with string that does not contain new line character(s). If a string contains a new line, it will be fooled to accept. For example, a valid format of a postal code may be 3 letters, 2 digits, and 2 letters (i.e. "abc99bc"). A string "abc99kk\ndoreme" would pass the validation because only the first line is tested! You have to ensure that the test string contains no new line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.