3

I am checking input character limit.

It's working fine on desktops but not working on mobile devices.

I also tried with on keydown but not happening.

I can use "e.which" but I am not getting "how I can do through it?"

Yes, it's possible through "onkeyup", but want to know how can I implement same functionality with "onkeyup"?

Please suggest me changes in my code.

My code snippet:

<script>
   $('#bank_ifsc_code').keypress(function(e){
                    var charLength = $(this).val().length+1;
                    if(charLength>11){ return false; }

                    var k = e.keyCode,
                    $return = ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || (k >= 48 && k <= 57));
                    if(!$return) { return false; }
   })
</script>
halfer
  • 19,824
  • 17
  • 99
  • 186
Rakesh Hiray
  • 721
  • 3
  • 15
  • Try the [onInput](https://www.w3schools.com/jsref/event_oninput.asp) Event or the [KeyUp](https://www.w3schools.com/jsref/event_onkeyup.asp) event. – Kryptur Jul 05 '19 at 11:51
  • yes its possible through "onkeyup" but I want to know the logic idea how can i do same functionality through "onkeyup".. – Rakesh Hiray Jul 05 '19 at 11:58

1 Answers1

0

Use the maxlength attribute for the length restriction.

<input id="bank_ifsc_code" maxlength="11" />

Then, in keyup, you can replace all unwanted characters with a regular expression for example.

$("#bank_ifsc_code").keyup(function(e) {
    var val = $(this).val();    
    val = val.replace(/[^a-zA-Z0-9]/, "");
    $(this).val(val);
});


JSFiddle

Kryptur
  • 745
  • 6
  • 17