Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:16:28+00:00 2026-06-13T21:16:28+00:00

I’ve the JavaScript code to validate a Zip code field in my page and

  • 0

I’ve the JavaScript code to validate a Zip code field in my page and that is as follows:

<script type="text/javascript">
 var zip=$("#zip").val();
if (zip == '') {
               errorMessage = REG_FIELD_EMPTY_ZIP;
               $('#msg_zip').html(errorMessage).show();
               }
               else {
                if ((zip.length) < 5) 
                    {
                    errorMessage = REG_FIELD_LENGTH_ZIP;
                $('#msg_zip').html(errorMessage).show();
                }
                    else 
                    {
                if (!reg.test(zip)) {
                errorMessage = REG_FIELD_ONLYDIGITS_ZIP;
                $('#msg_zip').html(errorMessage).show();
                }
               else {
                $('#msg_zip').html('').hide();
                    }
             }
           }
           </script>

Here, i want to add some more conditions as:
1> this zip code field should also support Canadian Zip-code format (A#A #A#) with or without space it should accept. If the value is not in the Canadian Zip-code format,
2> It should accept only 5 digits.
How can i modify the above script so that i can make my zip code field to accept canadian zip-code format as well.
By searching i’ve found some regular-expression to accept canadian zip-code as /^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$/; But when i tested it, it is not accepting the small case letters.
Can anyone clarify how to change my script so that it should accept the canadian-zip code or to accept only 5 digits. Thanks in Advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:16:29+00:00Added an answer on June 13, 2026 at 9:16 pm

    The regex you have for canadian zip-code is correct. If you want it to accept small case letters, you have to use it with case-insensitive option(i):

    (/^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$/i).test(zip)
    

    Regex for 5 digits:

    /^\d{5}$/
    

    Combining these two:

    (/^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$/i).test(zip) || (/^\d{5}$/).test(zip)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.