0

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)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sreekumar
  • 33
  • 1
  • 6

2 Answers2

1

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

jafarbtech
  • 6,842
  • 1
  • 36
  • 55
  • events can be fired but can you help me with validation, means i have to restrict user from entering any numeric value or special character, it not working – Sreekumar Nov 24 '16 at 06:58
  • please open the same fiddle link in your android device and check.. it not working (i am using ANDROID- Chrome Browser) – Sreekumar Nov 24 '16 at 07:23
  • for different questions dont edit it. try posting another question. we are here to help you. – jafarbtech Nov 24 '16 at 07:32
0

the best solution for android validations is using keyup function and validating using regex expression the code as follows(this regex only allowing alphabets) , note the android doesnt support preventDefault(); method

$( "#textbox" ).on("keyup", function(e) {
                         var inVal=" ";
              inVal=$(this).val();
            var regex = new RegExp(/^[a-zA-Z ]+$/);

          if(!(regex.test(inVal)||$(this).IsNumeric) ){
                 $(this).val($(this).val().replace(/[^a-zA-z\s]/gi, ''))

        }
        });