Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript String.fromCharCode Case Sensitivity?

Tags:

I am simply listening for a keyup event of an input element and gather the results into a string like so

word=word+String.fromCharCode(key.keyCode); 

The problem is that the word is in capital letters while I want it to be case-sensitive. For example if I type abcef my accumulated word becomes 'ABCEF' .

Note - I need a pure javascript solution (no libraries..) Any thoughts?

like image 552
Joel Blum Avatar asked Feb 18 '13 19:02

Joel Blum


People also ask

Is JavaScript string comparison case sensitive?

The most basic way to do case insensitive string comparison in JavaScript is using either the toLowerCase() or toUpperCase() method to make sure both strings are either all lowercase or all uppercase.

How do you check if a string contains a substring case insensitive JavaScript?

To do case insensitive search, you can use regular expressions and the String#match() function, or you can convert both the string and substring to lower case using the String#toLowerCase() function.

Is JavaScript case sensitive?

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

What is string fromCharCode in JavaScript?

The String. fromCharCode() method converts Unicode values to characters. The String. fromCharCode() is a static method of the String object.


1 Answers

Events like keyup and keydown will return 65 for both a and A (and also true for event.shiftKey if that key is held down).

The keypress event returns different keycodes for upper and lower case letters, so to get this working case sensitive you should use the keypress event, and fromCharCode() will return the correct letter, case sensitive.

like image 93
adeneo Avatar answered Sep 28 '22 04:09

adeneo