0

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:

enter image description here

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>
4
  • What if you try /[^A-Za-z0-9 ]/g.test(value) Commented Oct 16, 2019 at 11:48
  • Thanks your your comment. Unfortunately then I can type in disabled characters also after starting with an allowed character. Commented Oct 16, 2019 at 11:51
  • I can't type any non-allowed char when the field is empty in your fiddle. Commented Oct 16, 2019 at 11:57
  • I am using Google Chrome 77.x Commented Oct 16, 2019 at 11:58

2 Answers 2

1
  1. You don't have an oldValue when you enter any character for first time, so your code won't go into your second condition
  2. Once your first character doesn't match, whatever you type next, the value never goes into first condition so oldValue is never set and as there is no existing oldValue your code won't go into second condition.

You can have an initial oldValue to empty string to fix this issue.

Add textbox.oldValue = ""; before textbox.addEventListener.

Sign up to request clarification or add additional context in comments.

1 Comment

That was it! Thanks for the explanation.
1

This works for me:

function setInputFilter(textbox, inputFilter) {
  ["input"].forEach(function(event) {
    textbox.addEventListener(event, function() {
      if (inputFilter(this.value)) {
        if (this.hasOwnProperty("oldValue")) {
          this.value = this.oldValue;
          this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
        } else {
          this.value = "";
          this.setSelectionRange(0, 0);
        }
      } else {
        this.oldValue = this.value;
        this.oldSelectionStart = this.selectionStart;
        this.oldSelectionEnd = this.selectionEnd;
      }
    });
  });
}

window.addEventListener("DOMContentLoaded", function() {
  setInputFilter(document.getElementsByName("targetDocument")[0], function(
    value
  ) {
    return /[^A-Za-z0-9 ]/g.test(value);
  });
});
<input type="text" class="polarion-TextBox" name="targetDocument" style="width: 100%;">

Main change is clearing out the value if there is no oldValue to restore on invalid input. I also inverted the logic and return true if there are any invalid characters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.