Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    52
    Thanks
    0
    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?

    Code:
    <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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,082
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    I don't use unwieldy jQuery, but a simple regex to validate a UK postcode is:-

    Code:
    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

    Code:
    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
    •