Refactor valid length checks

A little less repetitive.