Search code examples
javascriptpolymerweb-component

Validate <paper-input> value on-change instead of on-key?


I've got a simple <paper-input> element. I'm using its pattern-attribute to validate it. It always validates when I enter a key (on-key). Instead I would like it to validate on-change

<paper-input error-message="Not a number"
             label="Enter a number"  
             pattern="\d*" 
             errorMessage="Not a number" 
             auto-validate></paper-input>

Solution

  • Remove the auto-validate attribute, and add an on-value-changed event listener that runs the paper-inputs validate function

    here is a demo on jsbin

    <paper-input id='input' error-message="Not a number" label="Enter a number" pattern="\d*" errorMessage="Not a number"></paper-input>
    
    <script>
      document.getElementById('input').addEventListener('value-changed', function(e) {
        // console.log(e.detail.value);
        e.currentTarget.validate();
      })
    </script>