Another obvious validation rule would be maximum number of words. E.g. "Your response in 25 words or less".
This is possible through some clunky regex but should be a pretty simple additional rule.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | wordcount_validation_6_1-3.patch | 2.03 KB | 8ballsteve |
Comments
Comment #1
wastrilith2k commentedI needed the opposite, a minimum number of words. Here is what I added to webform_validation.rules.inc:
In the function "webform_validation_webform_validation_validators()", I added the following code above line 49, that read "'max_length' => array("
'min_words' => array(
'name' => "Minimum number of words",
'component_types' => array(
'textfield',
'textarea'
),
'custom_data' => array(
'label' => t('Minimum number of words'),
'description' => t('Specify the minimum number of words that have to be entered to pass validation. Words are defined as strings of letters separated by spaces.')
),
'description' => t('Verifies that a user-entered value contains at least the specified number of characters'),
),
Then, to test, I added the following to the "webform_validation_webform_validation_validate" function:
case 'min_words':
$min_words = $rule['data'];
foreach ($items as $key => $val) {
if ($val && (count(preg_split("/[[:space:]]+/", trim($val))) < $min_words)) {
$errors[$key] = t('%item needs to be at least %num words long', array('%item' => $components[$key]['name'], '%num' => $min_words));
}
}
return $errors;
break;
above the "case 'max_length':" line.
I image you could do something essentially the same if you needed to have a maximum number of words.
I'd create a patch if I had the time to work out how to.
James
Comment #2
svendecabooterComment #3
paul-c commentedwastrilith2k's solution worked for me, with a few minor tweaks to set a maximum word limit. For 6x.1.3 make the changes in webform_validation.validators.inc.
Change this:
if ($val && (count(preg_split("/[[:space:]]+/", trim($val))) < $min_words))to
if ($val && (count(preg_split("/[[:space:]]+/", trim($val))) > $max_words))And change all instances of the variable $min_words to $max_words where required.
Change the various descriptions and instructions as needed, too.
Comment #4
katherinecory commentedI can confirm the above code does work. I inserted the following above line 49 ( 'max_length' => array ) in webform_validation.validators.inc:
I also insert the following above the now line 292 which starts with case 'max_length':
I wouldn't know how to make this into a patch though...
Comment #5
8ballsteve commentedThanks for this - works perfectly for me.
Created a patch for this too (see attached)
Thanks again
Comment #6
svendecabooterComment #7
svendecabooterIs there any reason why the POSIX style regex
[:space:]is used, rather than the Perl style\s?From what I can tell from php.net the latter is better supported in PHP, and is binary-safe.
Comment #8
svendecabooterThese 2 validation rules have been committed now.
I've used the Perl style regex. If this poses problems for someone, let me know.
You can do a git clone to get this functionality, or wait for the next release