I am trying to only allow certain characters a user can type / copy into a HTML-<input> field by using following Regex:
return /^[A-Za-z0-9 ]*$/.test(value);
It works for me properly, but only if I start with an allowed character (e.g.: "A").
However, if I am starting with a character which isn't allowed by my Regex (e.g.: "!"), I suddenly can type in characters which should be disabled:
For testing I have created following fiddle: >>Click<<
Full script as reference:
<input type="text" class="polarion-TextBox" name="targetDocument" style="width: 100%;">
<script type="text/javascript">
window.addEventListener("DOMContentLoaded", function() {
window.setTimeout ( function() {
// Restricts input for the given textbox to the given inputFilter.
function setInputFilter(textbox, inputFilter) {
["input"].forEach(function(event) {
textbox.addEventListener(event, function() {
if (inputFilter(this.value)) {
this.oldValue = this.value;
this.oldSelectionStart = this.selectionStart;
this.oldSelectionEnd = this.selectionEnd;
} else if (this.hasOwnProperty("oldValue")) {
this.value = this.oldValue;
this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
}
});
});
}
// Restrict input to digits and '.' by using a regular expression filter.
setInputFilter(document.getElementsByName("targetDocument")[0], function(value) {
return /^[A-Za-z0-9 ]*$/.test(value);
});
}, 1000);
}, false);
</script>

/[^A-Za-z0-9 ]/g.test(value)