admin管理员组文章数量:1415100
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');
}
});
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');
}
});
Share
Improve this question
asked Jun 18, 2018 at 11:57
Michael KraemerMichael Kraemer
691 silver badge7 bronze badges
3
- What exactly is your issue? – Krishna Prashatt Commented Jun 18, 2018 at 12:01
- Thanks for your answer. I want the input field validated. Everytime the input of the "field_robot"-field is changed, it should be checked, if the input is 42. – Michael Kraemer Commented Jun 18, 2018 at 12:04
- You can add a onInput attribute to the input field. And add the function with the validation – Krishna Prashatt Commented Jun 18, 2018 at 12:08
2 Answers
Reset to default 3The issue is you're declaring val
outside of your event listener. So, your value is never refreshed after the input. You need to retrieve the field's value inside the event listener.
See the snippet below :
var field = document.getElementById('field_robot');
field.addEventListener('input', function() {
var val = document.getElementById('field_robot').value;
if (val != '42') {
field.setCustomValidity('invalid');
} else {
event.target.setCustomValidity('');
}
});
<form id="myForm">
<input type="text" id="field_robot" name="">
<input type="submit" id="submit" name="submit">
</form>
Now, you can go a bit further and have a listener you could use for several input fields, using the event
object provided by the listener and a fieldValidator
function that would take your event and a boolean condition as parameters.
See this other snippet :
var field = document.getElementById('field_robot');
field.addEventListener('input', function(event) {
fieldValidator(event, (field.value == '42'));
});
function fieldValidator(event, condition) {
var val = event.target.value;
if (!condition) {
event.target.setCustomValidity('invalid');
} else {
event.target.setCustomValidity('');
}
}
<form id="myForm">
<input type="text" id="field_robot" name="">
<input type="submit" id="submit" name="submit">
</form>
So now it works well.
var field = document.getElementById('field_robot');
field.addEventListener('change', function() {
var val = document.getElementById('field_robot').value;
if (val != '42') {
field.setCustomValidity('invalid');
} else {
field.setCustomValidity('');
};
});
本文标签: javascriptForm Validation Set Custom ValidityStack Overflow
版权声明:本文标题:javascript - Form Validation Set Custom Validity - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745148471a2644808.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论