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.

CommentFileSizeAuthor
#5 wordcount_validation_6_1-3.patch2.03 KB8ballsteve

Comments

wastrilith2k’s picture

I 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

svendecabooter’s picture

Status: Active » Needs work
paul-c’s picture

Version: 6.x-1.2 » 6.x-1.3
Status: Needs work » Active

wastrilith2k'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.

katherinecory’s picture

I can confirm the above code does work. I inserted the following above line 49 ( 'max_length' => array ) in webform_validation.validators.inc:

'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'),
),
'max_words' => array(
'name' => "Maximum number of words",
'component_types' => array(
'textfield',
'textarea'
),

I also insert the following above the now line 292 which starts with case 'max_length':

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;
	case 'max_words':
		$max_words = $rule['data'];
		foreach ($items as $key => $val) {
		  if ($val && (count(preg_split("/[[:space:]]+/", trim($val))) > $max_words)) {
			$errors[$key] = t('%item can be maximum %num words long', array('%item' => $components[$key]['name'], '%num' => $max_words));
		  }
		}
		return $errors;
		break;

I wouldn't know how to make this into a patch though...

8ballsteve’s picture

StatusFileSize
new2.03 KB

Thanks for this - works perfectly for me.

Created a patch for this too (see attached)

Thanks again

svendecabooter’s picture

Status: Active » Needs review
svendecabooter’s picture

Is 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.

svendecabooter’s picture

Status: Needs review » Fixed

These 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

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.