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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:03:56+00:00 2026-06-09T13:03:56+00:00

I am using the RegEx ^[0-9]+$ to allow only digits. I want to allow

  • 0

I am using the RegEx ^[0-9]+$" to allow only digits.
I want to allow hyphens - & spaces also.

Can any one help me to modify the RegEx to a proper one?

I was using following JS code to achieve the same, but in this way if the user is copying & pasting the text then the key press is not getting accounted.

//Allowed characters are : 8 = Back space,
//9 = Horizontal tab, 27 = esc, 127 = Delete,
//13 = Enter digits = 48 to 57, 45 = hypen

$('#PostalCode').keypress(function (e) {
    var key = (e.keyCode ? e.keyCode : e.which);
    return (key == 8 || key == 9 || key == 127 || key == 27 || key == 13 || key == 45 || (key >= 48 && key <= 57));
});
  • 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-09T13:03:57+00:00Added an answer on June 9, 2026 at 1:03 pm

    You can add a - and space in the character class like so:

    ^[0-9 -]+$
    

    Be sure to put the - either in the back or front (as it won’t be mistaken for a range, then), or escape it:

    ^[0-9 \-]+$
    

    You could also use \d instead of 0-9, as they’re equivalent:

    ^[\d -]+$
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I allow a dot in an URL using Regex? Any advice? thanks
I am using this regex: preg_match('~[^A-Za-z0-9\+\-]~', $incoming); How can I add empty spaces here?
Using the following regex with the ExtJS library, the intention is to only allow
i'm using C# and i'm trying to allow only alphabetical letters and spaces. my
While using regex to help solve a problem in the Python Challenge , I
I'm playing with using Regex to modify Twitter posts. I'm currently attempting to trim
I need some help using regex in java. Im trying to achieve 2 different
I'm looking for a regex to validate initials. The only format I want it
Which regular expression can I use to match (allow) any kind of letter from
How to allow only specific set of HTML tags & specific set of Attributes

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.