I’m using the following validation to count words in rails (I got the example from the Rails docs), but it’s not real accurate:
validates :body, :length => {
:minimum => 50,
:maximum => 300,
:tokenizer => lambda { |str| str.scan(/\w+/) },
:too_short => "must have at least %{count} words",
:too_long => "must have at most %{count} words"
}
A user tried to post something that’s 291 words (that’s the count Word gives) and it was rejected as too long. I don’t know exactly what’s wrong with the expression that’s being used, or what might be a good expression to ensure an accurate word count.
Instead of scanning for /\w+/ you should scan for /\s+|$/ and decrease the max by one since the \w word character match can be unexpected with unusual characters since only A-Za-z0-9_- are valid.