admin管理员组文章数量:1357281
I may be approaching this the wrong way, but I'm confused how to extract information from an input
's validityState
object.
var input = document.querySelector('#myinput');
var validity = input.validity;
var keys = Object.keys(validity);
console.log( validity ); // [ object validityState ]
console.log( validity.valid ); // false (this is expected)
console.log( keys ) // empty array
console.log( keys.length ) // 0
<input type="text" id="myinput" required>
I may be approaching this the wrong way, but I'm confused how to extract information from an input
's validityState
object.
var input = document.querySelector('#myinput');
var validity = input.validity;
var keys = Object.keys(validity);
console.log( validity ); // [ object validityState ]
console.log( validity.valid ); // false (this is expected)
console.log( keys ) // empty array
console.log( keys.length ) // 0
<input type="text" id="myinput" required>
My main question is, how do I find out which property is "failing" if I can't iterate over the validityState
object?
Am I missing something?
Share Improve this question asked Oct 25, 2016 at 11:17 evolutionxboxevolutionxbox 4,1326 gold badges38 silver badges57 bronze badges1 Answer
Reset to default 12Do not use Object.keys()
.
You can simply iterate on your validity state like this:
var input = document.querySelector('#myinput');
var validity = input.validity;
for(var key in validity){
console.log(key + ": " + validity[key]);
}
<input type="text" id="myinput" required>
Edit:
Object.keys()
only referring to keys that are in the current Object and does not look in the prototype of this Object. Note thatobject.hasOwnProperty()
does the same testin
returntrue
if the property is present in the current Object OR if the property is present in the prototype
本文标签: javascriptSearching validityState for valuesStack Overflow
版权声明:本文标题:javascript - Searching validityState for values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743962737a2569329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论