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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:37:08+00:00 2026-05-17T19:37:08+00:00

Is there any existing jQuery functionality that can test if characters entered into a

  • 0

Is there any existing jQuery functionality that can test if characters entered into a textbox are either numeric, or valid in a number?

Such as

.00 or 0.00, but not 0.00.00 or 0a

What I’d like to do is catch any invalid characters before they appear in the textbox.

If it’s not possible with jQuery, what’s the best way to approach this?

I know with JavaScript I can test isNaN() and then return false, but that’s going to start getting hairy when I have to account for all possible keystrokes.

  • 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-17T19:37:09+00:00Added an answer on May 17, 2026 at 7:37 pm

    It’s a little tricky, since you want to make sure you can enter all numbers left to right, but something like this:

    $("input").keyup(function() {       
        this.value = this.value.match(/[-+]?[0-9]*\.?[0-9]*/);
    });
    

    Try it out with this jsFiddle

    Note how I’m checking the number from left to right. This means that + must be valid. Also 5. must be valid, or you could never enter 5.0 or +5.

    Now the above has some major issue (try the arrow keys).

    Here’s a slightly more elegant solution that accommodates a default value as well:

    $(function() {                      // <== DOC ready
    
        var prev="";                    // Initial value to replace default text with
    
        $("input").click(function () {  // Include a select on click
            $(this).select();           // if you have a default value
        });
    
        $("input").keyup(function() {  
    
            if(/^[-+]?[0-9]*\.?[0-9]*$/.test(this.value)) // If number....
                prev = this.value;                        // store it as the fallback
            else
                this.value = prev;                        // else go to fallback
        });
    });
    

    Try it out with this jsFiddle

    Example HTML for the above:

    <input type="text" value="Enter only a number" />
    

    Note how when you use .test() you have to test from the beginning ^ to the end $.

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

Sidebar

Related Questions

Is there any way that we can mock certain methods using existing objects ?
Is there any existing solution to embed the app.config file into the resulting assembly?
Are there any existing add-ons which would provide the functionality Upload image from local
Are there any existing libraries in existence that will parse a datetime from a
is there any existing function in jquery library with the help of which I
I'm wondering if there are any existing (ideally well documented) PHP or Javascript/jQuery libraries/plugins
I've started looking at jQuery's built-in tabs functionality here: http://docs.jquery.com/UI/Tabs Is there any way
I'm using jQuery to create an element that exactly overlays any existing one. That's
Is there any javascript plugin/library/script that will can transform a standard PHP to use
Is there any JavaScript function that can unify $ function from both Prototype 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.