2

i have an input text field and want users type it with English keyboard not with Arabic or etc. How can i do that in HTML or Javascript or jquery?

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

i tested it with lang attribute like this,but it didn't work:

<input type="text" id="username" size="15" lang="en">
2
  • Use some JS to handle keypresses and filter out any non-English characters you don't want. JS cannot detect what keyboard is in use, but it can detect the characters as they're entered. Commented Feb 14, 2012 at 20:58
  • 1
    Keypress handlers can be circumvented by using cut and paste with the mouse. All JavaScript code can be circumvented by disabling JavaScript in the browser. So handling the data server-side should be prepared to any characters and do whatever needs to be done with them. It’s not clear at all what the real problem or goal is here. Commented Feb 14, 2012 at 21:09

1 Answer 1

3

Pure javascript solution would be:

<script language="JavaScript">
function checkKeycode(e) {
  var keycode=e.keyCode? e.keyCode : e.charCode
  if(isEnglishKeyCode(keycode)){
    //do something if u want
  }
  else{
    void(0);
  }
}
</script>
<form>
<input type="text" onkeydown="checkKeycode(event); this.select()" />
</form>

So void(0) "disables" the event and it wont be written into the input. I trust that u can write the isEnglishKeyCode(keycode) method. For help on keycode u can use this link or search around.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.