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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:26:21+00:00 2026-05-25T10:26:21+00:00

I need to validate a (four digits) postcode field that can accept: A single

  • 0

I need to validate a (four digits) postcode field that can accept:

  1. A single postcode (e.g. 4009) -> it has to be four digit
  2. Multiple postcodes separated by comma(s) (e.g. 4002,5001)
  3. A range of postcodes using hyphen (e.g. 4000-4010)
  4. Any combination of #1 and #3 separated by comma(s) (e.g. 4000,4002,4005-4010,5001)

Another example of #4 will be: 4000-4007,5000

How can I do validation using regex in C#. My question is more about how to construct the pattern itself. Thank you.

Updates

This is what I came up with (using your inputs on regex):

public override bool IsValid(object value)
{
    if (value == null)
    {
        return true;
    }

    var stringValue = value.ToString().Replace(" ", "");

    foreach (var pc in stringValue.Split(','))
    {
        if(!Regex.Match(pc, @"^(\d{4})$|^(\d{4}-\d{4})$").Success)
        {
            return false;
        }
    }

    return true;
}

I was kind of hoping to do the validation without having to do string.Split(). I am not sure if that’s possible at all. If anyone has better idea, please post it here. Thank you.

  • 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-25T10:26:22+00:00Added an answer on May 25, 2026 at 10:26 am

    What about ^((\d{4})([,-])?)+$

    Edit: That one matched on 80789809 aswell

    New: ^(\d{4})(([,-])(\d{4}))*$

    Tested against:

    4000,4002,4005-4010,5001 yes
    4000-4007,5000 yes
    4000-4010 yes
    4002,5001 yes
    4009 yes
    40004002,4005-1010,5001 no
    4000-4007, 5000 no
    40004010 no
    4002,50012345 no
    4009, no
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need validate multiple fields in the JavaScript Framework, but can't figure out how.
I need to validate two fields only if a third field has a specific
I have a text box I need to validate that.. I mean user can
I need to validate that certain variables can create a valid datetime and if
I need to validate a date/time field on a webpage but want it to
I don't need to validate that the IP address is reachable or anything like
I’m using a commercial application that has an option to use RegEx to validate
I need to validate a textbox that it should allow only certain characters and
I have a constructor that need to validate passed data. public Rational(int m, int
I have a long spreadsheet with tariff codes that I need to validate and

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.