Sreekumar Sreekumar - 8 days ago 10
Android Question

key press event of html is not working in mobile devices



function keyDown()
{
alert("KeydownEvent Fired");
}
function keypress()
{
alert("keypress Event Fired")
}
function keyup()
{
alert("key up event fired");
}
function onlyAlphabets(e) {
debugger;
try {
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e) {
var charCode = e.which;
}
else { return true; }
var keyCodes = [13, 16, 18, 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 91, 92, 93, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99,
100, 101, 102, 103, 104, 105, 144, 145];
if (keyCodes.indexOf(charCode) != -1) {
return false;
}
else if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || charCode == 8 || charCode == 32 || charCode == 9 || charCode == 46 || charCode == 37 || charCode == 39 || charCode == 16 || charCode == 17)
return true;
else
return false;
}
catch (err) {
alert(err.Description);
}
}

<input type="textbox" placeholder="Keydown" onkeydown="return onlyAlphabets(event,this);"></input>
</br>
<input type="textbox" placeholder="Keypress" onkeypress="keypress()"></input>
</br>
<input type="textbox" placeholder="Keyup" onkeyup="keyup()"></input>
</br>





This is just a piece of code.

I have include a Some input tags which all have Different keypress events and then i have uploaded it to live, it working fine in all the browser's.
but when i try to open my page in Mobile or tablet it is not working fine.

Even if alerts are working in Browsers but i have kept a validation for first name on keydown that user can only enter alphabets(numeric and special character not allowed)

Answer

In mobile browser both onkeydown and onkeyup will work. I think you have simple syntax mistakes that might be the problem

Check this fiddle in you mobile. 2 events are firing other than onkeypress

DEMO:-

<script>
function keyDown()
{
alert("KeydownEvent Fired");
}
function keypress()
{
alert("keypress Event Fired")
}
function keyup()
{
alert("key up event fired");
}
</script>
<input type="textbox" placeholder="Keydown" onkeydown="keyDown()"/>
<br/>
<input type="textbox" placeholder="Keypress" onkeypress="keypress()"/>
<br/>
<input type="textbox" placeholder="Keyup" onkeyup="keyup()"/>
<br/>

EDIT:-

To add validation You have to handle in onkeydown. check this fiddlefor details

Comments