Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-15-2012, 11:52 PM   PM User | #1
millsy007
New Coder

 
Join Date: Feb 2009
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
millsy007 is an unknown quantity at this point
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>
millsy007 is offline   Reply With Quote
Old 10-16-2012, 08:31 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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)
__________________

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.

Last edited by Philip M; 10-16-2012 at 08:35 AM.. Reason: typo
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:01 PM.


Advertisement
Log in to turn off these ads.