Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2009
    Thanked 0 Times in 0 Posts

    JQuery Postcode Validation Problem

    I have some code that should check for a correctly formatted UK postcode, however I can't get it to carry out the check, I click submit and nothing happens. I was thinking I have a syntax error but I have tried everything I can with no success?

    <script src="/jquery/jquery.js"></script>
    <script src="/jquery/jquery.validate.js"></script>
                                            function(value, element, regexp) {
                                                    var check = false;
                                                    var re = new RegExp(regexp);
                                                    return this.optional(element) || re.test(value);
                                            "Please enter a valid postcode."
        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.")
    <form id="commentForm" method="get" action="">
         <label for="cpostcode">Postcode</label>
         <em>*</em><input id="cpostcode" name="postcode" size="25" />
         <input class="submit" type="submit" value="Submit"/>

  2. #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,599 Times in 2,577 Posts
    I don't use unwieldy jQuery, but a simple regex to validate a UK postcode is:-

    if (/^[A-Z]{1,2}\d{1,2}[A-Z]?\s\d[A-Z]{2}$/.test(postcode.value)) {    // UK postcode including SW1A 1AA (space required) - must be in caps

    However, a more sophisticated regex which disallows postcodes which do not exist such as AC or DF is

    if(/((^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(^WC[1-2])|(^EC[1-4])|(^SW1[ABEHMNPRVWXY])|(^GIR\s?0AA))(\s\d[ABDEFGHJLNPQRSTUWXYZ]{2})$/.test(postcode.value)) { //  UK POSTCODES  Letters C, I, K, M, O and V are never used in the incode.  Must be in caps.
    If you have a syntax error try using your error console.

    “The old believe everything; the middle aged suspect everything; the young know everything.” - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)
    Last edited by Philip M; 10-16-2012 at 08:35 AM. Reason: typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts