Is it possible to alter the charCode received during a keypress or keydown events

Can javascript alter the character value received by a keypress event?  keypress can filter out characters by returning true or false, but the question is can it Alter the character returned for display in the textbox?
glkubsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
No you CAN'T. User need to be the master of its browser, not the hacker behind the screen

But you can :
event.preventDefault();
append an other character of your choice
0
Alexander Eßer [Alex140181]Software DeveloperCommented:
No you CAN'T. User need to be the master of its browser, not the hacker behind the screen

seriously?!? ;-)

window.onload = function () {
    var input = document.getElementById("mytext");

    input.onkeypress = function () {
        var evt = arguments[0] || event;
        var char = String.fromCharCode(evt.which || evt.keyCode);

        // Is it a period?
        if (char == ".") {
            // Replace it with a comma
            input.value += ",";

            // Cancel the original event
            evt.cancelBubble = true;
            return false;
        }
    }
};

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
glkubsAuthor Commented:
Alex,

Thank you for an almost perfect solution... I'm using jquery, so for some reason I had to change the evt.cancelBubble to use evt.preventDefault();  Otherwise, I got the original character and the one I was adding!  (simple lower to upper case at the moment).

Thank you... This makes filtering user input easy!
0
leakim971PluritechnicianCommented:
seriously?!? ;-)
input.value += ",";

I had to change the evt.cancelBubble to use evt.preventDefault();

But you can :
event.preventDefault();
append an other character of your choice


Ahem...
0
Alexander Eßer [Alex140181]Software DeveloperCommented:
But you can :
event.preventDefault();
append an other character of your choice

Ahem...

Btw: You should really provide a little bit more information that might guide the asker to a suitable solution ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.