I have it in a custom module, but if you want feel free to use it
/**
* Implementation of hook_webform_validation_validators().
*/
function attiks_webform_validation_validators() {
return array(
'validEAN' => array(
'name' => "EAN check",
'component_types' => array(
'textfield',
),
'description' => t('EAN code check.'),
'custom_error' => TRUE,
),
);
}
/**
* Implementation of hook_webform_validation_validate().
*/
function attiks_webform_validation_validate($validator_name, $items, $components, $rule) {
if ($items) {
switch ($validator_name) {
case 'validEAN':
$valid_ids = array(1, 2, 3); // get this from database source instead
foreach ($items as $key => $val) {
if (!empty($val)) {
if (drupal_strlen($val) > 13) {
$errors[$key] = t('%item is not valid, too many digits', array('%item' => $components[$key]['name']));
}
else {
if (drupal_strlen($val) < 13) {
$val = str_repeat('0', drupal_strlen($val) - 13) + $val;
}
if (!attiks_ean13_valid($val)) {
$errors[$key] = t('%item is not valid, please check again', array('%item' => $components[$key]['name']));
}
}
}
}
return $errors;
break;
}
}
}
function attiks_ean13_valid($digits){
$even = $digits[1] + $digits[3] + $digits[5] + $digits[7] + $digits[9] + $digits[11];
$even = $even * 3;
$odd = $digits[0] + $digits[2] + $digits[4] + $digits[6] + $digits[8] + $digits[10];
$total = $even + $odd;
$checksum = $total % 10;
if ($checksum != 0) {
$checksum = 10 - $checksum;
}
return $digits[12] == $checksum;
}
Comments
Comment #1
attiks CreditAttribution: attiks commentedComment #2
Liam MorlandI think something as specific as this should go in its own project. You could start by making it a sandbox project.
Comment #3
g089h515r806 CreditAttribution: g089h515r806 commentedI have ported it to Field validation module.
Comment #4
Liam MorlandDrupal 6 is no longer supported. If this issue exists in a later version of webform_validation, please re-open and update the version number.
Comment #5
Liam MorlandComment #6
Liam Morland