82 code for "r" or "R"

http://jsfiddle.net/4s3wxrp6/

I want to disable all numbers after && but the 82 code for r is possible only write with big R not lowercase r allowed but the code is 82? Then why not allowed lowercase to type in input?

img

Thanks!

Can you provide an example user input and what want to happen?

Well the following line:

if (charCode > 82 && (charCode < 48 || charCode > 57))

After && set to write only numbers? As I understand 48 is 0 and 57 is 9 is ok. But I want still include the 82 code the R and r characters still to allow to write in input?

http://jsfiddle.net/4s3wxrp6/

With above jsfiddle is possible to write only capitalized R not lowercase r as 82 code.

Thanks!

Your condition does not make sense. If charCode is greater than 82 there is no way it can also be less than 48, so using && here is not correct. If charCode is greater than 82, then charCode will already be greater than 57, so this is not needed.

If the goal is to only allow letters (regardless of case), then you can either do:

function isNumber(evt) {
  evt = (evt) ? evt : window.event;
  var charCode = (evt.which) ? evt.which : evt.keyCode;
  return charCode >= 65 && charCode <= 90 || charCode > 97 && charCode <= 122;
}

OR

function isNumber(evt) {
  evt = (evt) ? evt : window.event;
  var charPressed = String.fromCharCode(evt.which ? evt.which : evt.keyCode);
  return /[a-z]/i.test(charPressed);
}
1 Like

Above code was to display if write not numbers typed.

Please look there

https://jsfiddle.net/zac7rf1L/

Is with .replace and is also replace all letters is fine. But how can accept that a plus character to not replace just stay like numbers?

Thanks!

So you only want to be able to type numbers or the + character into the input field?

You are right! Sorry for unclear. I try many with a regexp tester but really not want to do. :joy:

Thanks!

$('input').on('keyup', function() {
  $(this).val($(this).val().replace(/[^\+0-9]/g, ''));
});

Notice the keyup, so you capture and replace after the character has been typed instead of trying to remove a character that has not actually been typed yet (because the keyup event has not taken place). The only thing I do not like with this approach is that it briefly shows the character and then removes it. The better approach is what you originally had where you checked the key actually pressed.

1 Like