I'm playing around with form validations. I've build a simple captcha field and I want to check it via javascript.
At this point it works static. I need to add an eventlistener, but don't know how.
Hope, you can help.
HTML
<form>
<input type="text" id="field_robot" name="">
<input type="submit" id="submit" name="submit">
</form>
JS
var val = document.getElementById('field_robot').value;
var field = document.getElementById('field_robot');
field.addEventListener('input', function(){
if (val != '42') {
field.setCustomValidity('invalid');
}
});