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

  • Home
  • SEARCH
  • 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 8169555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:52:44+00:00 2026-06-06T20:52:44+00:00

I need to restrict an input so that the user can only enter dollar

  • 0

I need to restrict an input so that the user can only enter dollar amounts. This means that they can enter a string of digits, optionally followed by a decimal and 0-2 more digits. No commas, no dollar signs.

I found what I think is a good pattern for this, but I also want to allow the user to use backspace, enter, tab, etc. I was hoping for a better way to do this than explicitly listing every valid keycode on my whitelist. I was hoping that /\W/ would do the trick, but alas, it matches too many keypresses.

This is what I currently have.

  $input.keypress(function(evt) {
    var ch = String.fromCharCode(evt.which);
    var val = evt.target.value + ch;
    var pattern = /^\d+(\.?(\d{1,2})?)$/;
    if (!pattern.test(val)) {
      evt.preventDefault();
    }
  });

If there’s a smart and clean way to allow the user to make corrections and to navigate the form with the keyboard, I’d be glad to hear it.

If you have a suggestion for improving the Regex above, please feel free to include that in your answer (or leave it in a comment if you don’t have an answer).

Thanks!

  • 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-06T20:52:46+00:00Added an answer on June 6, 2026 at 8:52 pm

    When I do this in my code, I don’t run the regex on the keypress itself but on the contents of the field they are typing into. That way you don’t need to worry about non-printing characters. You will need to worry about them cut-n-pasting into the field which will not fire that keypress event too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to restrict user access to filebrowser using permissions. For example, only users
I basicly need to be able to restrict the items that can be appended
I need to restrict access to my application to only one specific user account.
I need to restrict a custom widget setFont() so that it only accepts fixed-width
I need to restrict respond_to to JSON only, because I am constructing a web
I need to make sure that every users accessing my web application can do
I need to restrict cookies to my www subdomain, this works by me adding
I need to restrict the values that are available in a DynamicFilter control for
I need to restrict a controller in CI 2 to only run from command
I am using Sql Server-2008. I need to restrict user access to TABLES (to

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.