MozillaZine


Cant Copy or paste using ctrl+c ctrl+v

Discuss how to use and promote Web standards with the Mozilla Gecko engine.
anish.too.cool
 
Posts: 3
Joined: January 30th, 2015, 4:32 am

Post Posted January 30th, 2015, 4:40 am

i have a form in which in one textbox i am restricting user to enter chars only numbers. using the below javascript
Code: Select all
 $('#practiceNPI').bind('keypress', function (e) {
       
       
        var k = e.which;
        var ok = k >= 48 && k <= 57 || k == 01//select
            || k == 0
            || k == 8                                 
        ; 

        if (!ok) {
            e.preventDefault();
     
       }
    });



By doing this every thing works well but the only ISSUE is with mozilla where i cant use keybord shortcut to copy paste or select
for ex:- try to copy paste in this field by ctrl+v or ctrl+c http://jsfiddle.net/lesson8/HkEuf/1/

Plz help.. want to use that function...Thx in Adv..

trolly
Moderator

User avatar
 
Posts: 39909
Joined: August 22nd, 2005, 7:25 am

Post Posted January 30th, 2015, 6:57 am

I think you should find out which key codes are ctrl-c/ctrl-v and let them pass.
01 seems to be ctrl-A so ctrl-c is 03 and ctrl-v is 22 (or 23).
Think for yourself. Otherwise you have to believe what other people tell you.
A society based on individualism is an oxymoron. || Freedom is at first the freedom to starve.
Constitution says: One man, one vote. Supreme court says: One dollar, one vote.

anish.too.cool
 
Posts: 3
Joined: January 30th, 2015, 4:32 am

Post Posted January 30th, 2015, 10:23 pm

trolly wrote:I think you should find out which key codes are ctrl-c/ctrl-v and let them pass.
01 seems to be ctrl-A so ctrl-c is 03 and ctrl-v is 22 (or 23).



Try that also but didnot work.Passed all of them ](*,) ](*,)

Dom1953
 
Posts: 52
Joined: July 24th, 2014, 6:02 am
Location: Australia

Post Posted February 1st, 2015, 5:08 pm

Keyboard event object properties and behaviour lack standardisation, with the Level 3 Event specification still only a working draft. No surprise you'vel come across browser dependent differences in keyboard event handling. For the record, the "which" property of a keyboard event does not currently appear in the Level 3 working draft, the nearest property being event.code which is supposed to be of type string and giving the Unicode value of a key pressed.

But for the purposes of this case, e.which is giving the value of the key pressed without consideration of modifier keys (alt or ctrl) pressed. I got your JSFiddle case to work (I think as expected) in Firefox by adding a line to let control keys through:
Code: Select all
$(document).ready(function () {
  //called when key is pressed in textbox
  $("#quantity").keypress(function (e) {
    // let the browser process control keys
     if(e.ctrlKey)
          return true;
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        //display error message
        $("#errmsg").html("Digits Only").show().fadeOut("slow");
               return false;
    }
   });
});

anish.too.cool
 
Posts: 3
Joined: January 30th, 2015, 4:32 am

Post Posted February 1st, 2015, 9:47 pm

Dom1953 wrote:Keyboard event object properties and behaviour lack standardisation, with the Level 3 Event specification still only a working draft. No surprise you'vel come across browser dependent differences in keyboard event handling. For the record, the "which" property of a keyboard event does not currently appear in the Level 3 working draft, the nearest property being event.code which is supposed to be of type string and giving the Unicode value of a key pressed.

But for the purposes of this case, e.which is giving the value of the key pressed without consideration of modifier keys (alt or ctrl) pressed. I got your JSFiddle case to work (I think as expected) in Firefox by adding a line to let control keys through:
Code: Select all
$(document).ready(function () {
  //called when key is pressed in textbox
  $("#quantity").keypress(function (e) {
    // let the browser process control keys
     if(e.ctrlKey)
          return true;
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        //display error message
        $("#errmsg").html("Digits Only").show().fadeOut("slow");
               return false;
    }
   });
});




Thanks a lot man...save my day..tryed a lot of think but missed the simple one.. :P .WORked like a champ... =D> =D>

Return to Web Development / Standards Evangelism


Who is online

Users browsing this forum: Bing [Bot] and 1 guest