ends the loop in isValid when valid word is found