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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:40:29+00:00 2026-06-14T19:40:29+00:00

I have a textbox where I want only allowed up to 11 digits, an

  • 0

I have a textbox where I want only allowed up to 11 digits, an optional comma, and two more digits after it. Anything else should not be rendered when the key is pressed into the textbox:

$('#txt').keypress(function (e) {
  var code = e.which;
  var key = String.fromCharCode(code);
  // REGEX TO AVOID CHARS & A DOT (.)
  var pattern = /[a-zA-Z]|\./g;
  var isMatch = pattern.test(key);
  if (isMatch) {
    // DO NOT RENDER CHARS & dot
    e.preventDefault();
  }
});

The above code works when an invalid key is pressed, such as a char or a dot, but does not ensure only one comma and only 2 digits afterwards.

This must match:

12314
123123,44

This must not:

12313,6666

Here is a demo.

UPDATE:
Any digit except numbers and comma must be avoided, so the regex I proposed is not valid since only prevents dots (.).

  • 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-14T19:40:30+00:00Added an answer on June 14, 2026 at 7:40 pm

    You should test your complete string, not only the current letter.

    $('#txt').keypress(function (e) {
        var key = String.fromCharCode(e.which);
        var pattern=/^[0-9]{1,11}(,[0-9]{0,2})?$/;
    
        // test this
        var txt = $(this).val() + key;
    
        if (!pattern.test(txt)) {
            e.preventDefault();
        }
    });
    

    ​

    jsfiddle example

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

Sidebar

Related Questions

I have textbox to enter only alphabet and numerical values. I do not want
I want to only have the calendar to specify my textbox input, and not
I have a textbox that should accept datetime formats only. I am not sure
I have two CheckBoxes and a TextBox. I want to TextBox to have IsEnabled=False
I have a ComboBox and a TextBox and want to enable/disable TextBox only, when
I want a regular expression for a textbox which allows only 3 digits and
I have a certain textbox and I want to add a div after it.
I have one TextBox in my UserControl. Here I want enter only positive or
I want my textbox to have only floating point value, and filter out any
I have a string in a textbox and want only one of the words

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.