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

attiks’s picture


/**
* 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;
}

Liam Morland’s picture

I think something as specific as this should go in its own project. You could start by making it a sandbox project.

g089h515r806’s picture

I have ported it to Field validation module.

Liam Morland’s picture

Status: Needs review » Closed (won't fix)

Drupal 6 is no longer supported. If this issue exists in a later version of webform_validation, please re-open and update the version number.

Liam Morland’s picture

Category: Task » Feature request
Liam Morland’s picture

Status: Closed (won't fix) » Closed (outdated)