I want to validate an input field with regex (replacing all vowels). The input should always be matched against that regex, regardles if typed into or pasted into that field.
I know how to deal with the typing part, but I need help for copy - paste.
All vowels should be skipped (e.g.: "abcde" copy --> "bcd" paste)
$('document').ready(function(){
var pattern = /[aeiou]/ig;
$("#input").on("keypress keydown", function (e) {
if (e.key.match(pattern) !== null) {
e.preventDefault();
}
});
$("#input").on("paste", function (e) {
var text = e.originalEvent.clipboardData.getData('Text');
e.preventDefault();
//TODO... replace all vowels
console.log(text);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='text' id='input'/>
At first i tried to replace them and just set the value with $('#input').val($('#input').val() + copiedValue)
, but this only works if the cursor is at the end of the input.