Question

The validity property of an HTML input in HTML5 does not seem to work in Firefox.

var input = $input.get(0);
console.log(input.validity)

This console log returns an empty object. It's working on Chrome and Safari and I think it's also suppose to work on Firefox if I look at that doc?

https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement

Any ideas?

Était-ce utile?

La solution

If it's appearing as an empty object, it's likely because only enumerable properties are being rendered, which ValidityState's properties don't seem to be in Firefox.

But, you should still be able to access individual properties:

console.log(input.validity.valid);   // true/false
console.log(input.validity.tooLong); // true/false
// etc.

Autres conseils

input.validity returns a special object, not just a simple boolean.

So, use input.validity.valid to check if an inputted value is correct.

See this JSFiddle to see validity properties.

And this MDN Article about the ValidityState object.

And as far as debugging in Firefox, Firebug shows this kind of stuff.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top