diff --git a/modules/webform_validation_no_ccs/webform_validation_no_ccs.info b/modules/webform_validation_no_ccs/webform_validation_no_ccs.info new file mode 100755 index 0000000..7be3db0 --- /dev/null +++ b/modules/webform_validation_no_ccs/webform_validation_no_ccs.info @@ -0,0 +1,3 @@ +name = Webform Validation No Credit Card Numbers +core = 7.x +dependencies[] = webform \ No newline at end of file diff --git a/modules/webform_validation_no_ccs/webform_validation_no_ccs.js b/modules/webform_validation_no_ccs/webform_validation_no_ccs.js new file mode 100755 index 0000000..7b64d34 --- /dev/null +++ b/modules/webform_validation_no_ccs/webform_validation_no_ccs.js @@ -0,0 +1,71 @@ +//jQuery wrapper +(function ($) { + //Define a Drupal behaviour with a custom name + Drupal.behaviors.webform_validation_no_ccsAddNoCcs = { + attach: function (context) { + //Add an eventlistener to the document reacting on the + //'clientsideValidationAddCustomRules' event. + $(document).bind('clientsideValidationAddCustomRules', function(event){ + //Add your custom method with the 'addMethod' function of jQuery.validator + //http://docs.jquery.com/Plugins/Validation/Validator/addMethod#namemethodmessage + jQuery.validator.addMethod("noCcs", function(value, element, param) { + //return true if valid, false if invalid + //return !(valid_credit_card(value)); + var returnBoolean = true; + var val = ''; + + + value = value.replace(/\D/g,''); + + + + while(value.length >= 15) { + + if (value.length >= 16) { + val = value.substr(0, 16); + + if (valid_credit_card(val)) { + returnBoolean = false; + } + } + val = value.substr(0, 15); + + if (valid_credit_card(val)) { + returnBoolean = false; + } + value = value.substr(1); + } + return returnBoolean; + + //Enter a default error message. + }, jQuery.format('Value must a valid phone number')); + }); + } + } + + function valid_credit_card(value) { + // accept only digits, dashes or spaces + if (/[^0-9-\s]+/.test(value)) return false; + + // The Luhn Algorithm. It's so pretty. + var nCheck = 0, nDigit = 0, bEven = false; + value = value.replace(/\D/g, ""); + + for (var n = value.length - 1; n >= 0; n--) { + var cDigit = value.charAt(n), + nDigit = parseInt(cDigit, 10); + + if (bEven) { + if ((nDigit *= 2) > 9) nDigit -= 9; + } + + nCheck += nDigit; + bEven = !bEven; + } + + return (nCheck % 10) == 0; +} + + + +})(jQuery); \ No newline at end of file diff --git a/modules/webform_validation_no_ccs/webform_validation_no_ccs.module b/modules/webform_validation_no_ccs/webform_validation_no_ccs.module new file mode 100755 index 0000000..2ab5ac1 --- /dev/null +++ b/modules/webform_validation_no_ccs/webform_validation_no_ccs.module @@ -0,0 +1,102 @@ + array( + 'name' => t("No Credit Card Numbers"), + 'component_types' => array( + 'textfield', + 'textarea', + 'number' + ), + 'description' => t("Keep credit card numbers from hitting the database"), + ) + ); +} + +/** + * Implements hook_webform_validation_validate(). + */ +function webform_validation_no_ccs_webform_validation_validate($validator_name, $items, $components, $rule) { + if ($items) { + switch ($validator_name) { + case 'no_ccs': + foreach ($items as $key => $val) { + if (check_for_valid_luhn($val)) { + $errors[$key] = t('%item contains a credit card number', array('%item' => $components[$key]['name'])); + } + + } + if(isset($errors)) { + return $errors; + } + break; + } + } +} + + +/** this will check the string as long as its at least 15 digits long against the first 15 and (perhaps 16) digits against the luhn check. +if we get one positive, we $returnboolean indicating that **/ + +function check_for_valid_luhn($val) { + $val = preg_replace("/[^0-9]/", "", $val); + $returnboolean = 0; + if (!is_numeric($val)) { + return 0; + } + else { + + settype($val, 'string'); + + while (strlen($val) >= 15) { + if (strlen($val) >= 16) { + if (is_valid_luhn(substr($val, 0, 16))) { + $returnboolean = 1; + } + } + if (is_valid_luhn(substr($val, 0, 15))) { + $returnboolean = 1; + } + $val = substr($val, 1); + } + return $returnboolean; + } +} + + +/** Luhn Check Boolean **/ + +function is_valid_luhn($number) { + + $sumTable = array( + array(0,1,2,3,4,5,6,7,8,9), + array(0,2,4,6,8,1,3,5,7,9)); + $sum = 0; + $flip = 0; + for ($i = strlen($number) - 1; $i >= 0; $i--) { + $sum += $sumTable[$flip++ & 0x1][$number[$i]]; + } + return $sum % 10 === 0; +} + + + + +/** + * Implementation of hook_clientside_validation_rule_alter(). + */ +function webform_validation_no_ccs_clientside_validation_rule_alter (&$js_rules, $element, $context) { + switch ($context['type']) { + case 'webform': + if ($context['rule']['validator'] == 'no_ccs') { + drupal_add_js(drupal_get_path('module', 'webform_validation_no_ccs') . '/webform_validation_no_ccs.js'); + $title = variable_get('clientside_validation_prefix', '') . $element['element_title'] . variable_get('clientside_validation_suffix', ''); + $js_rules[$element['element_name']]['noCcs'] = TRUE; + $js_rules[$element['element_name']]['messages']['noCcs'] = theme('clientside_error', array('message' => t('Field !name must not contain any credit card numbers.'), 'placeholders' => array('!name' => $title))); + } + break; + } +} \ No newline at end of file