Get length dynamically

It is possible that maxlength attribute gets changed. Therefore it makes
sense to check for it each time explicitly.

Note that this commit contains a tiny formatting fix.