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 4622826
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:53:53+00:00 2026-05-22T02:53:53+00:00

I am trying to validate postcode field using jQuery but it does not work.

  • 0

I am trying to validate postcode field using jQuery but it does not work.

Here is my code:

if (postcod.val().match ('~^([1-9]{1}[0-9]{3}\s[A-Z]{2})$~')){
         }else{
            postcod.addClass("needsfilled");
            postcod.val(postcoderror);
        }

Here is html:

PostCode<br /><input id="postcod" type="text" value="" name="postcod" />

Could you help plz

  • 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-05-22T02:53:55+00:00Added an answer on May 22, 2026 at 2:53 am

    There are a few issues…

    • Regexs are not quoted in JavaScript, unless you need to create a RegExp object via its constructor. In this circumstance, you don’t. I only use them if I need to concatenate an outside string. While you can pass a string to match() and it will be implicitly converted, it’s not recommended over passing a RegExp literal.
    • You need to use / as the delimiters in a regex literal. When using the RegExp, you don’t pass any delimiters. So ~ are never correct. Perhaps you are thinking PHP.
    • Whilst not necessary, some of your regex could be improved. The {1} quantifier is implicit, and the [0-9] character class can be substituted with \d.
    • Instead of having an empty block and an else, just negate the condition with the bang operator (!).

    Here is how I might use it…

    if ( ! postcod.val().match(/^([1-9]\d{3}\s[A-Z]{2})$/)){
       ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m trying to validate input by using egrep and regex.Here is the line from
I'm trying to validate a input field with the JQuery validation plugin. The numeric
I am trying to validate html select element using jQuery Validate plugin. I set
I'm trying to validate a form, but doesn't work :\ , When I submit
I am trying to validate the form input but my validation is not working.
I am trying to validate a form, using jQuery validate and show errors messages
I'm trying to validate my form before inserting into database with this code, but
I am trying to validate a form, but I can't get any code to
I'm trying to validate the following XML but I'm unable to, can you please
My form is submitted by a link using JavaScript, but I am also trying

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.