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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:55:20+00:00 2026-05-17T17:55:20+00:00

Is it possible to change the character which has been entered on keypress, without

  • 0

Is it possible to change the character which has been entered on keypress, without doing it manually?

For example, if I want to force uppercase letters based on some condition, it’d be nice to do the following:

function onKeypressHandler(e)
{
    if ( condition )
    {
        e.which -= 32;
    }
}

But of course that doesn’t work.

NOTE: This is not an across the board uppercasing, but only specific characters.

Maybe I want to say if ( e.which >= 97 && e.which <= 102 ) or if ( Wind.Direction == 'South' ) or whatever – the condition itself is not important, but the uppercasing must only apply to the current character not the entire input.

I can do it by manually appending the changed character, but this is an ugly and messy way of doing it, and probably slower than it could be.

function onKeypressHandler(e)
{
    if ( condition )
    {
        $j(this).val( $j(this).val() + String.fromCharCode( e.which - 32 ) );
        return false;
    }
}

A specific flaw with this method – if selecting all input text and entering a key, if it drops into this then it doesn’t remove existing content, but simply appends to the content the user wanted removed. (Would need to investigating detecting any selected text to solve that, which makes this one even uglier.)

Can anyone provide a better solution?

  • 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-17T17:55:21+00:00Added an answer on May 17, 2026 at 5:55 pm

    The following will do the job. It’s based on an answer I wrote to another question. Customize the transformTypedChar function to suit your needs; my example capitalizes only the letters a-g.

    If you need this on a textarea rather than an <input type="text"> then be aware that there are issues in IE <= 8 with line breaks that the following code doesn’t handle for the sake of brevity. You can find the cross browser function for obtaining the selection within a textarea here: Is there an Internet Explorer approved substitute for selectionStart and selectionEnd?

    function transformTypedChar(charStr) {
        return /[a-g]/.test(charStr) ? charStr.toUpperCase() : charStr;
    }
    
    document.getElementById("your_input_id").onkeypress = function(evt) {
        var val = this.value;
        evt = evt || window.event;
    
        // Ensure we only handle printable keys, excluding enter and space
        var charCode = typeof evt.which == "number" ? evt.which : evt.keyCode;
        if (charCode && charCode > 32) {
            var keyChar = String.fromCharCode(charCode);
    
            // Transform typed character
            var mappedChar = transformTypedChar(keyChar);
    
            var start, end;
            if (typeof this.selectionStart == "number" && typeof this.selectionEnd == "number") {
                // Non-IE browsers and IE 9
                start = this.selectionStart;
                end = this.selectionEnd;
                this.value = val.slice(0, start) + mappedChar + val.slice(end);
    
                // Move the caret
                this.selectionStart = this.selectionEnd = start + 1;
            } else if (document.selection && document.selection.createRange) {
                // For IE up to version 8
                var selectionRange = document.selection.createRange();
                var textInputRange = this.createTextRange();
                var precedingRange = this.createTextRange();
                var bookmark = selectionRange.getBookmark();
                textInputRange.moveToBookmark(bookmark);
                precedingRange.setEndPoint("EndToStart", textInputRange);
                start = precedingRange.text.length;
                end = start + selectionRange.text.length;
    
                this.value = val.slice(0, start) + mappedChar + val.slice(end);
                start++;
    
                // Move the caret
                textInputRange = this.createTextRange();
                textInputRange.collapse(true);
                textInputRange.move("character", start - (this.value.slice(0, start).split("\r\n").length - 1));
                textInputRange.select();
            }
    
            return false;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to change a UTF-8 character (which is in a gchar array), so
Possible Duplicate: Query about Html <input type=password> tag…? I want change the character displayed
Is it possible to change the size of a font in .net winforms without
Is it possible to change the newline character the .readline() method looks for while
Possible Duplicate: Change icon on of an App in iOS 4 in run-time Is
Possible Duplicate: change layout and template in zend dynamically according to the device(android tab,iphone,ipad)
Is it possible to change the assembly name based on the project configuration? I
Is it possible to change the name(the one that apears under 'processes' in Task
Is it possible to change the way that Resharper formats properties? I don't like:
Is it possible to change the starting value on an axis for a chart

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.