0

Is it possible to call some javascript function instantly once the pattern is matched in input field? Is there any specific event for that?

<input type="text" class="form-control" name="zipcode" pattern="^[1-9][0-9]{3}\s?[a-z|A-Z]{2}$" (keyup.enter)="checkSth()">

I would like to call this checkSth() immediately without clicking enter when the pattern is matched.

1011sophie
  • 237
  • 3
  • 9

1 Answers1

-1

You can add an event listener in your javascript like such

html

<input id="input" />

js

document.querySelector('#input').addEventListener('keyup', () => console.log('changed'))

Example: https://jsfiddle.net/ew8ywk7y/

Baruch
  • 2,381
  • 1
  • 17
  • 29
  • That fires whenever the field is changed. The question was asking how to run it when the pattern is matched. – Quentin Oct 02 '17 at 09:46
  • What would be the difference between this and what he's already doing with the `keyup` attribute? Furthermore, it's impossible to do what he's trying to accomplish, he can validate the regexp inside the event listener. – Baruch Oct 02 '17 at 09:46
  • @Baruch you should type atSignUsername (@+Quentin) to inform he from your comment! – S.Serpooshan Oct 02 '17 at 10:44