SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Postal Code validation (Canadian)

    I am using

    <script>
    function Validate()
    {
    var regEx = /[a-zA-Z][0-9][a-zA-Z](-| |)[0-9][a-zA-Z][0-9]/;
    if(regEx.test())
    {
    alert('Valid Postal Code');
    }
    else
    {
    alert('Invalid Postal Code');
    }
    }

    </script>

    to validate the postal code with an onBlur function for some reason even if i type in a valid postal code it comes as invalid. Can anyone clarify why?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You need to provide the value of the postalcode field to regEx.test()

    i.e. regEx.test(document.getElementById("postalcode").value)

    Assuming the postal code field has the id "postalcode"

    I'm assuming btw the regex is correct, I don't know how canadian postal codes are formatted.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Member
    Join Date
    May 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you so much it works perfectly fine now


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
  •