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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:58:35+00:00 2026-06-01T05:58:35+00:00

I am trying to do a value replace in an input field. It is

  • 0

I am trying to do a value replace in an input field. It is almost working correctly. The problem is it adds the value to the end of input when I only want it added to the one location

Html:

<input id="boom" value="03/15/0212" />​

Jquery: I have hard coded some values to keep from placing all functions

$('#boom').on('keypress', function (e)
{
    var crs = this.selectionStart;
    var from;
    var to;
    var sub;
    _dateSection = 'month';
    var currentObj = this;
    var currentVal = $(this).val();
    var c = String.fromCharCode(e.which);
    var stringSplit = currentVal.split('/');
    switch (_dateSection) {
        case 'month':
            _currentDateValue = stringSplit[0];
            from = 0;
            to = 2;
            sub = currentVal.substring(from, to);
            break;
        case 'day':
            _currentDateValue = stringSplit[1];
            from = 3;
            to = 6;
            sub = currentVal.substring(from, to);
            break;
        case 'year':
            _currentDateValue = stringSplit[2];
            from = 6;
            to = 10;
            sub = currentVal.substring(from, to);
            break;
    };
    var check = false;
    if (!check) {
        $(currentObj).val(function (index, value)
        {
            return value.replace(sub, c);
        });
    }
});

JsFiddle

If you add 3 to the month portion of the input which is currently populated with 03 it replaces is correctly with 3, but it also adds 3 to the end of the year. Making the year, 20123 when the year value should remain 2012.

The final value should be

3/15/2012

not

3/15/20123

Edit:

this code gives the same problem:

$(currentObj).val(currentVal.replace(sub, c));

as does this

var replaceValue = currentVal.replace(sub, c)
$(currentObj).val(replaceValue);
  • 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-01T05:58:36+00:00Added an answer on June 1, 2026 at 5:58 am

    The extra character that is added to the end is added by the input field itself. The keypress event is handled by the input also, which does the normal thing and adds the character to the value.

    Just put e.preventDefault(); in your event handler to keep the input from getting the event.


    Also, don’t use replace to put the character in the string, use substr to get the parts of the string that you want to keep:

    value = value.substr(0, from) + c + value.substr(to, c);
    

    If the string is for example 05/05/2012 and you try to replace the day with 4, it will instead replace the first occurace and you get 4/05/2012 instead of 05/4/2012.

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

Sidebar

Related Questions

I am trying to select the input field that is added when pressing the
I am trying to add this HTML also when a input field is added:
I have next problem, when I'm trying to retrieve value from xforms:select elements I
I'm trying to replace inputs with spans containing the input values in such a
I've been trying to get the Count for my Value field in my query
I am trying to replace the $3 column values of the input file if
I'm trying to validate an input field and store it using AJAX My JavaScript
I am trying to change the value on a hidden field in a WebBrowser
I have a hidden input field that will take the value of another on
I am trying to add value to CoreData from .plist and get from coreData

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.