how to prevent "post accent" in input text

thiagoprzy

I believe this is an easy one, but I couldn't find any answer after a couple of hours searching on google (maybe I wasn't able to use the correct words in the search :-P)

I have a javascript method that prevents the user to fill the textbox with other characters than numbers, as it can be seen in the code below, and it's used in KeyDown event:

function checkNumberInput(e) {
  // Allow: backspace, delete, tab, escape, enter and .
  if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||
      // Allow: Ctrl+A, Command+A
      (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) ||
      // Allow: home, end, left, right, down, up
      (e.keyCode >= 35 && e.keyCode <= 40)) {
    // let it happen, don't do anything
    return;
  }
  // Ensure that it is a number and stop the keypress
  if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
    e.preventDefault();
  }
}

The problem is that, if the user types accent characters that "waits" a second key (such as ~´`^), it appears in the textbox when a number is typed right after.

For example:

a) types 1 => [1________]
b) types ~ => [1________]
c) types 3 => [1~3______]

How can I prevent this from happening? Thanks in advance.

Al.G.

Do you mean that ' and a are converted to а́? This is built in the system, you cannot change that.
What you can do is check the input after that. This way you'll also prevent paste with mouse:

$('input').on('keydown', checkNumberInput).on('blur focus', function(){
    var val = $('input').val();

    // now remove everything that is not allowed
    val = val.replace(/[^0-9.]+/g, '');

    $('input').val(val);
})

(The exact implementation is deffinitely not perfect, just to illustrate what's going on)

https://jsfiddle.net/5afwdhzx/

About really instant fix - give this closure a name and call it inside the keyDown function with some short delay, like @haroldo said above.

https://jsfiddle.net/5afwdhzx/1/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular- How to prevent text in input from showing if input is invalid

How to prevent reading input from text field if text field is disabled

How to prevent a user from removing the first three characters in a text input?

How to prevent or remove foreground text input from background Java process?

Prevent backspace in input text box

Prevent select on input text field

IE 10, 11. How to prevent the triggering of input events on focus from text input with placeholder?

Prevent text from shifting in an input box

Validation to prevent duplicated values on input text

Prevent text from wrapping around input

How to prevent backspace symbols in input?

How to prevent autoscroll to focused input?

How to Prevent useState & useLocation from re rendering in input/text field in react js?

How to prevent a semicolon from being entered into html text input, but allowing a colon?

How do I prevent placeholder text from being vertically centered in a large height input form element?

How do I prevent a flexbox from becoming wider when it contains a text input field?

how can I prevent a user to type 0 as the first character in a text input using vanilla javascript?

How to prevent a read-only input text field from disappearing when disabled during dark mode?

How to add fixed placeholder text to an input box and prevent the user from deleting it on a web page?

Prevent space in input from both text input and copy and paste

Prevent input when text fills visible length of input

How to prevent user from copy-pasting text with invalid characters into input text field immediately on copy-paste action?

How to prevent item text wrap

How to trim input text

How to align text and input?

How to prevent the html details element from toggling when the summary has an embedded text input and user presses space bar

how to prevent user not to enter alphabets in input fied?

How to prevent an input from being disabled?

How to prevent wrong user input on a variable?