I have a working safecracker entryform. My client would like visitors to write at least X words before submitting the entry.

I would like to show the number of words a visitor has typed in real time (on key up). Once the amount of words has reached X the visitor should be able to submit the entry.

I've looked at the docs: http://ellislab.com/expressionengine/user-guide/modules/safecracker/index.html#form-validation. They pointed me to http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#rulereference. The closest rule I found is: min_length. This only gives me a char count.

I'd like to be able to validate by number of words.


If it's on keyup, you need to handle it in the browser, with jQuery.

There are two jQuery plugins here. If validating on word count is not supported, you can write custom validation functions for either. You'd want to leverage what they're doing for validating minimum character length, split on spaces, and then get the length of that array. The length of the array would be the number of words.

It sounds like you're looking to do more than just validate, though –

I would like to show the number of words a visitor has typed in real time (on key up).

Validate implies that there would be an error message when you tab out of the field (onblur) or submit the form (onsubmit). You might need to do a bit more work than just leverage the validator plugin to display the current word count as the user is typing.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.