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 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

Anyone know a good solution for filtering input using jQuery? I'd like to do
Does anyone know of a good function out there for filtering generic input from
When receiving user input on forms I want to detect whether fields like username
Filtering QuerySets in Django work like the following: Entry.objects.filter(year=2006) How can I use filter
I am filtering the HKEYS by using Hook filtering function, I use the following
<script type=text/javascript language=javascript> $(function() { $(#distributor).autocomplete({ source: function(request, response) { $.ajax({ url: /Devices/autoDistributor, type:
i have database table with few text fields which i use to filter data
While working on filtering my NSMutableDictionary based on user input, I created the following
I'm working my way through input filtering (files) and I just discovered that if
I want to use the new PHP 5 > input filtering to sanitize 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.