3

OK, here's the stuff: I got one element

<input type="text" id="username" size="15">

and I need this element to force switch keyboard layout from ANY other language to English when users type something in this element. I DO NOT need to send it to the server. I simply need to force switch the language while typing in it.

Does anyone have a simple JS solution for it?

Here's what I've been thinking about: Theoretically. If i knew the language it shoud switch from, then i could write a JS which would replace every symbol to English

Under
  • 31
  • 1
  • 4
  • What does that even mean "switch language" – cport1 Jan 16 '15 at 01:56
  • What if the language was in english characters? –  Jan 16 '15 at 01:57
  • I mean when you start typing in ANY OTHER language, the keyboard should switch to English – Under Jan 16 '15 at 01:58
  • That may not be possible. See related questions [here](http://stackoverflow.com/questions/12595970/iphone-change-keyboard-language-programmatically) and [here](http://stackoverflow.com/questions/12303593/change-keyboard-input-language). – showdev Jan 16 '15 at 02:06
  • 1
    There is a `lang` attribute that html elements take but documentation says it is not useful. In other words it does nothing according to http://www.w3schools.com/tags/att_global_lang.asp. but you could try to use it and see if the mobile browser respects it and pops up English character set instead of user locale. I don't think there is any JS. To my knowledge, what you are asking for can only be achieved through some sort of meta level attribute that will indicate to the browser the language it should use, if at all there exists such a way. – praneetloke Jan 16 '15 at 02:13
  • I DO NOOOOOOOOT NEED TO SEND IT TO THE SERVER – Under Jan 16 '15 at 02:15

1 Answers1

0

Do you just mean to only allow alphanumeric characters? If so, you could do something like this.

$('#username').keyup(function() {
                if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
                    this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
                }
            });

As for actually changing the keyboard layout itself, I don't believe that would be possible through javascript...

さりげない告白
  • 1,397
  • 2
  • 13
  • 26
  • I dont think it's possible either... I tried just in case... But! Theoretically. If i knew the language it shoud switch from, then i could write a JS which would replace every symbol to English – Under Jan 16 '15 at 02:13
  • Even if it was just limited to Japanese, that would be extremely difficult to be accurate with... 何時 can be read as either itsu (depending on your standardization of romaji, even itu) or nanji -- spaces also aren't used between words, which would prove to be another difficulty. IMEs often have shortcuts for words or phrases, so when I type in Japanese I only have type the first part of it, and click the suggestion (especially for words/phrases I type often) so you couldn't do a raw input conversion either. – さりげない告白 Jan 16 '15 at 02:18
  • luckily I don't need Janapese. But i guess when you press ONE button on your keyboard which represents one symbol in Japanese then it could be replaced with ONE corresponding symbol in the English keyboard layout – Under Jan 16 '15 at 02:23
  • You could do that, for example な to na -- but what if someone types hanzawanaoki and ends up clicking that character's catchphrase 100baigaesida!!! (an actual example from my phone's IME) -- it's completely different from what they typed. -- I don't know much about other languages IMES though, honestly... – さりげない告白 Jan 16 '15 at 02:27
  • well. take a look at this pic http://en.wikipedia.org/wiki/Keyboard_layout#mediaviewer/File:KB_Japanese.svg when you press ONE button on your keyboard then it automatically replaces that with the corresponding ONE in English. I think that's possible with onkeyup attribute and JS – Under Jan 16 '15 at 02:30
  • It'd be possible with mapping yes, but the IME would still be visible and filled with nonsense until the user hits escape or enter. However, if they are using romaji input rather than kana input, it'd make for a weird typing experience -- and if someone as accessing it from a phone, it'd make no sense with what was produced from the Japanese input (they can easily switch to english characters, but the conversion itself would hold no meaning) – さりげない告白 Jan 16 '15 at 02:39