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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:02:32+00:00 2026-06-15T21:02:32+00:00

I have a textbox where I’m using jQuery to limit the input to numeric

  • 0

I have a textbox where I’m using jQuery to limit the input to numeric characters only. The issue I’m having is that when the 4 digit code starts with a 0, it gets deleted from the textbox on focusout or when another number is entered. It’s an mvc3 application and the datatype is string. How do I keep the leading zero from being deleted? Is there something I’m not noticing in my jQuery code? This also applies if there are multiple leading zeros for example, “0025”.

$('#selector').bind('keyup paste', function (e) {
if (checkArrowKeyEntry(e)) {
    var ob = $(this);
    if (e.type == 'paste') {
        setTimeout(function () {
            myFuncion(ob);
        }, 1);
    } else {
        myFuncion(ob);
    }
}
});

function myFuncion(ob) {
ob.val(numericInputOnly(ob.val()));
}

function checkArrowKeyEntry(e) {
isNotArrowKey = true;
if (e.keyCode == 37 || e.keyCode == 39) { //|| e.keyCode == 8 || e.keyCode == 46) {
    // leftarrow, rightarrow, delete, backspace
    isNotArrowKey = false;
}
return isNotArrowKey;
}

function numericInputOnly(inputvalue) {
if (!isNaN(parseInt(inputvalue, 10))) {
    inputvalue = parseInt(inputvalue, 10);
}
else {
    inputvalue = "";
}
inputvalue = inputvalue.toString().replace(/[^0-9]/g, '');
return inputvalue;
}
  • 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-15T21:02:34+00:00Added an answer on June 15, 2026 at 9:02 pm

    I suspect there is something wrong in the numericInputOnly function.

    You correctly make sure that it always outputs a string (so that the leading 0s do not get trimmed out), but you convert the inputValue to a string too early.

    the bug in your code is on the line :

    inputvalue = parseInt(inputvalue, 10);
    

    converting to an int will “lose” the trailing 0s.

    Try for example with

    function numericInputOnly(inputvalue) {
        return inputvalue.replace(/[^0-9]/g, '');
    }
    

    this will keep only the digits that were typed (all digits including leading 0s) and might correspond to what you need.

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

Sidebar

Related Questions

I have textbox that I want to run some jquery when the textbox loses
I am using jquery on asp.net mvc. I have textbox on page and I
I am using vb.net code and sql server 2005. I have textbox in my
have a TextBox item (MyTextBox) on a TabItem control. I have code that looks
I have TextBox which allow insert only numeric values (filtering), But when I paste
I am using C#.Net. I have textbox which allow only number, decimal and percentage(%)
I have a textbox whose input is being handled by jQuery. $('input.Search').bind(keyup, updateSearchTextbox); When
I have a textbox in a form that I created. I want to add
I want to have image button for browse and upload I have textbox using
i have textbox controls that are created dynamically in the ASP.NET. I want to

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.