SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    UK postcode validation

    Hi

    I am trying to submit a form using the code found on this page:

    http://javascript.internet.com/forms...alidation.html

    to achiege uk post code validation, it works, the only problem i am facing is that the form actually processes even if the post code is wrong.

    i.e. even if the post code is worng and it alerts the user that the post code is wrong, when you press the "ok" button in the "alert box" the form continues processing, it should stay still, it should not process.

    Please kindly advise.

    I have tweaked the code a little bit by adding onclick="postit()" to my submit button area and matched the name of the form and fields etc. i.e. doucment.myformname.myfieldname etc which should not be the prblem?

    Thanks in advance.

    k

  2. #2
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should be returning 'false' from your postit() function to stop the form submitting.

    You should also have an 'onsubmit' handler for the form.

  3. #3
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks + extra

    Quote Originally Posted by HarryR View Post
    You should be returning 'false' from your postit() function to stop the form submitting.

    You should also have an 'onsubmit' handler for the form.
    HI Harry,

    Thanks for advice, I got it to work, in addition to your advice to returning "false" as supposed to true and form handling as "on submit", i also had to add "return" to "on submit", i have pasted the code below in case anyone might need this.

    In this example, my form is called "cashapplication" and my post code field in form is called "postcode", you will need to change these to match your own form and respective field names.

    Code for header section:

    Code HTML4Strict:
    <head><SCRIPT LANGUAGE="JavaScript">
     
    <!-- This script and many more are available free online at -->
    <!-- The JavaScript Source!! [url]http://javascript.internet.com[/url] -->
    <!-- Original code by Peter Haydon -->
    <!-- [email]peter_haydon@lineone.net[/email] -->
     
    <!-- Begin
    function postit(){ //check postcode format is valid
     test = document.cashapplication.postcode.value; size = test.length
     test = test.toUpperCase(); //Change to uppercase
     while (test.slice(0,1) == " ") //Strip leading spaces
      {test = test.substr(1,size-1);size = test.length
      }
     while(test.slice(size-1,size)== " ") //Strip trailing spaces
      {test = test.substr(0,size-1);size = test.length
      }
     document.cashapplication.postcode.value = test; //write back to form field
     if (size < 6 || size > 8){ //Code length rule
      alert(test + " is not a valid postcode - wrong length");
      document.cashapplication.postcode.focus();
      return false;
      }
     if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
       alert(test + " is not a valid postcode - cannot start with a number");
       document.cashapplication.postcode.focus();
       return false;
      }
     if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
       alert(test + " is not a valid postcode - alpha character in wrong position");
       document.cashapplication.postcode.focus();
       return false;
      }
     if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
       alert(test + " is not a valid postcode - number in wrong position");
       document.cashapplication.postcode.focus();
       return false;
      }
     if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
       alert(test + " is not a valid postcode - number in wrong position");
       document.cashapplication.postcode.focus();
       return false;
      }
     if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
       alert(test + " is not a valid postcode - no space or space in wrong position");
       document.cashapplication.postcode.focus();
       return false;
       }
     count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
     if (count1 != count2){//only one space rule
       alert(test + " is not a valid postcode - only one space allowed");
       document.cashapplication.postcode.focus();
       return false;
      }
      return true;
    }
    //  End -->
    </script></head>

    and codes for form parameter section:

    Code HTML4Strict:
    <form name="cashapplication" method="POST" action="***.asp" onsubmit="return postit()">

    remember in my example the field in my form is named "postcode"

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This seems like an incredibly verbose way of validating a UK postcode. Why not just use a regular expression?

    I just wrote this one and it seems to work OK:
    Code:
    ^[a-zA-Z]{1,2}[\d]{1,2}[a-zA-Z]?\s?\d[a-zA-Z]{2}$
    It takes into account central London postcodes such as WC1H 9EF, which yours doesn't at the moment, and it works for postcodes with or without the space.

    Also, even if it's a "valid" postcode, it doesn't mean it exists. If you really want to be accurate, you should compare it against a database of existing UK postcodes, which you need to pay for. However, limited versions (only the first part) exist, e.g. http://www.easypeasy.com/guides/article.php?article=64

  5. #5
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for taking the time Raffles

  6. #6
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a good reference for regular expression notations?
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  7. #7
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    http://regular-expressions.info

    Very good resource.

  8. #8
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.


Bookmarks

Posting Permissions

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