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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:48:25+00:00 2026-05-27T16:48:25+00:00

I wanted to restrict the user from entering the values from a specific range.

  • 0

I wanted to restrict the user from entering the values from a specific range. So i wrote the following code using JQuery 1.7.1

var reg = /^[0-9]{1,4}[.]{0,1}[0-9]{0,2}$/g;

$("#txt" + filterID).bind('keypress', function (e) {
    var nn = $("#txtValues");
    var strValue = nn[0].value.toString();
    strValue = $.trim(strValue);
    var bool = reg.test(strValue);
    if (strValue.length == 0 && !((e.which < 48 || e.which > 57) && e.which != 46 && e.which != 8 && e.which != 0)) {
        return true;
    }
    else if (bool) {
        return true;
    }
    else { 
        e.preventDefault();
    }
}); 

When i test the input box, it’s not working as expected. It supposed to allow a floating point number with 2 digits after decimal point.Some of the valid formats are

1
1.
1.0
0
1.20
0.0
1.23
123.43
1234.12

I am not sure, where I have done wrong. After recognizing 1. it’s getting failed. Can anybody help me to identify the issue?

  • 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-27T16:48:26+00:00Added an answer on May 27, 2026 at 4:48 pm

    The keypress event is fired before the value has updated. Use the following code, which translates the event.which property to a character using the String.fromCharCode method.

    Non-printable keystrokes will generate a event.which of zero, which is certainly not a digit or dot.

    As for the RegExp, you have to group the expression after the dot, and add a questionmark, to say “Let this group match be optional”.

    var reg = /^[0-9]{1,4}(\.[0-9]{0,2})?$/;
    
    $("#txt" + filterID).bind('keypress', function (e) {
        var nn = $("#txtValues");
        var strValue = nn[0].value.toString() + String.fromCharCode(e.which);
        strValue = $.trim(strValue);
        var bool = reg.test(strValue);
        if (bool) {
            return true;
        }
        else { 
            e.preventDefault();
        }
    });
    

    The pattern will match the following:

    1
    1234
    1234.5
    But not:
    1235.    (Change {0,2} to {1,2} if you want to reject this match)
    123456
    1234.567
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to allow the user to choose a currency from a list of
Wanted an easy way to extract year month and day from a string. Using
Wanted to know if someone had a suggestion on code or maybe there's a
I wanted to generate one fix view using interface builder, but the size of
in .net Claim-based identity framework If i wanted to restrict users to do an
in the example code we're able to get events from a JSON feed by
I wanted to know if there's any way by which one could restrict the
I'm using Google's Geocoder to find lat lng coordinates for a given address. var
Wanted to write a program to implement a dictionary of words using Tries Data
Wanted to try something new, using the awesome twitter bootstrap and their Less CSS

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.