Only allow delete key

3

I have the following script, which blocks all typing in an input, forcing the user to use DatePicker made available:

            $(".readonly").keydown(function(e){
            e.preventDefault();
            });

In this same script, is there any way I can only allow the delete ?

    
asked by anonymous 09.08.2017 / 21:11

2 answers

3

Yes, you can do it as follows

$(".readonly").keydown(function(e){
    if( e.keyCode !== 46 ){ // 46 é tecla delete  
        e.preventDefault();
    }
});
    
09.08.2017 / 21:20
0

The code for the delete key is 46. You will only run e.preventDefault(); if it is different from delete . Every event in Jquery is given a parameter, in this case e , in it you can access the keycode of the .keydown event, so just check with the value of the delete key.

$(".readonly").keydown(function(e){
   if(e.keyCode != 46 ){
       e.preventDefault();
   }
});
    
09.08.2017 / 21:36