0

I am using maxlength in my Cordova based Android App. I have implemented maxlength using this code-

 $('.limit-eight').keyup(function(e) {
        if (e.keyCode != 8 || e.keyCode != 46) {
            if ($(this).val().length >= 8) {
                $(this).val($(this).val().substr(0, 8));
            }
        }
    });

but whenever user copies and pastes, text pastes as it is without restriction.

Muhammad Usman
  • 1,366
  • 5
  • 18
  • 33
nickalchemist
  • 2,211
  • 6
  • 31
  • 58

2 Answers2

1

Attach on paste event

  $('.limit-eight').on('paste', function() {
      // do your logic here 
       console.log('text pasted!')
  })​
Muhammad Usman
  • 1,366
  • 5
  • 18
  • 33
0

So, you want the onchange event to fire on keydown, blur, and paste? That's magic.

If you want to track changes as they type, use "onkeydown". If you need to trap paste operations with the mouse, use "onpaste" (IE, FF3) and "oninput" (FF, Opera, Chrome, Safari1).

Origin ref.: Best way to track onchange as-you-type in input type="text"?

Community
  • 1
  • 1