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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:54:03+00:00 2026-05-24T15:54:03+00:00

I am filtering input in input text fields, for example: And I am filtering

  • 0

I am filtering input in input text fields, for example:

And I am filtering input on the fly, like removing non-numeric characters.

$("#txtlevel").bind('keyup', function(evt)
{
    var value = $(this).val();
    value = value.replace(/\D/g, '');   /* Remove any non-digit characters */
    if ( value.length > 1 )
    {
        value = value.replace(/^[ 0]/g,'');   /* Remove any leading spaces or zeros */
    }
    $(this).val(value);
}

This works great in Firefox, but in IE and Chrome the cursor jumps to the end of the input field every time. How can I prevent this or reset the cursor position? I’ve seen code to do it for textfields but not input text fields.

  • 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-24T15:54:05+00:00Added an answer on May 24, 2026 at 3:54 pm

    When you use keyup and do regex replace (in other words, setting the value), the cursor will jump to the end. If your goal is to allow only numeric keys (plus backspace, delete), try this:

    $("#txtlevel").bind('keydown', function(event) {
        // Allow only backspace, delete, left and right arrows, return and tab
        if (event.keyCode == 46 || 
            event.keyCode == 8 || 
            event.keyCode == 37 || 
            event.keyCode == 39 || 
            event.keyCode == 13 || 
            event.keyCode == 9) {
            // let it happen
        }
        else {
            // stop the keypress if not a number
            if (event.keyCode < 48 || event.keyCode > 57 ) {
                event.preventDefault(); 
            }   
        }
    });
    

    To cancel out non-numeric events on paste, bind it to paste event:

    $("#txtlevel").bind('paste', function(e){ 
        var value = $(this).val();
        value = value.replace(/\D/g, '');   /* Remove any non-digit characters */
        if ( value.length > 1 ) {
            value = value.replace(/^[ 0]/g,'');   /* Remove any leading spaces or zeros */
        }
        $(this).val(value);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The url would be something like this: www.example.com/index.php?file=myFile.ext The filtering would only allow leters
Revised Code jQuery(function($) { $(#filter).keyup(function () { var filter = $(this).val(), count = 0;
We're letting users search a database from a single text input and I'm having
Scenario: I'm trying to intercept paste events inside a textarea/input text, and filter the
Output or Input filtering? I constantly see people writing filter you inputs, sanitize your
I am filtering user input in this way $id= intval($_REQUEST['id']); I am accepting only
I've looked at Sonar for a way to include custom fields for grouping/filtering data
I have code similar to this filtering entries in an Array of Objects: var
I want to do some basic filtering on a file. Read it, do processing,
Are there performance issues when retrieving and filtering data from views than from tables

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.