How to block special characters coming from a "Paste"?

Asked

Viewed 345 times

1

I have the following problem, there is a validation being made that way:

$(document).ready(function(){
  $(this).on("paste", "#email", function(event) {
     var $obj = $(this);
        setTimeout(function() {
            var str  = $obj.val();
              $obj.val(str.replace(/[^a-za-uA-ZA-U0-9_@\.\'\b  \t]/g, ''));
            }, 2);
   });
});

When you paste some text with special characters the function of an replace in the special characters by emptiness. Is there any way to block the special characters of Paste without using the replace?

My biggest problem is that the user can see the characters inside the input before they are overwritten, the validation is ok, but I am looking for some other way that the user does not "see" the replace happening.

  • But you’re no longer blocking them? you’ve blocked them by switching to the void. What you really want is to exchange them? [áäà] => a

  • @Guilhermelautert , in the current way the user can see the pasted characters before they receive the replace, I wish it was not possible to even paste some text with special characters

  • I believe that regex is not what you are looking for so there is no regex method that replaces or blocks the special characters beforehand, maybe you can implement the updating of this method to each character typed, just as they do in Cpf formatting when the user type the number and is added "." in 4th, 8th, 12th position and "-" to separate the last 2 digits from the other.

  • Tries to change the event, has no other p/ put in place of PASTE

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.