2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
|
|
|
* CreditCard field, contains validation and formspec for creditcard fields.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class CreditCardField extends TextField {
|
|
|
|
|
|
|
|
function Field() {
|
|
|
|
$parts = explode("\n", chunk_split($this->value,4,"\n"));
|
|
|
|
$parts = array_pad($parts, 4, "");
|
2011-12-22 13:10:57 +01:00
|
|
|
|
|
|
|
// TODO Mark as disabled/readonly
|
2007-07-19 12:40:28 +02:00
|
|
|
$field = "<span id=\"{$this->name}_Holder\" class=\"creditCardField\">" .
|
2008-04-06 06:08:45 +02:00
|
|
|
"<input autocomplete=\"off\" name=\"{$this->name}[0]\" value=\"$parts[0]\" maxlength=\"4\"" . $this->getTabIndexHTML(0) . " /> - " .
|
|
|
|
"<input autocomplete=\"off\" name=\"{$this->name}[1]\" value=\"$parts[1]\" maxlength=\"4\"" . $this->getTabIndexHTML(1) . " /> - " .
|
|
|
|
"<input autocomplete=\"off\" name=\"{$this->name}[2]\" value=\"$parts[2]\" maxlength=\"4\"" . $this->getTabIndexHTML(2) . " /> - " .
|
|
|
|
"<input autocomplete=\"off\" name=\"{$this->name}[3]\" value=\"$parts[3]\" maxlength=\"4\"" . $this->getTabIndexHTML(3) . " /></span>";
|
2007-07-19 12:40:28 +02:00
|
|
|
return $field;
|
|
|
|
}
|
2012-03-07 17:45:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get tabindex HTML string
|
|
|
|
*
|
|
|
|
* @param int $increment Increase current tabindex by this value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getTabIndexHTML($increment = 0) {
|
|
|
|
$tabIndex = (int)$this->getTabIndex() + (int)$increment;
|
|
|
|
return (is_numeric($tabIndex)) ? ' tabindex = "' . $tabIndex . '"' : '';
|
|
|
|
}
|
2011-12-21 17:35:42 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function dataValue() {
|
|
|
|
if(is_array($this->value)) return implode("", $this->value);
|
|
|
|
else return $this->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function jsValidation() {
|
|
|
|
$formID = $this->form->FormName();
|
2008-04-18 06:28:37 +02:00
|
|
|
$error1 = _t('CreditCardField.VALIDATIONJS1', 'Please ensure you have entered the');
|
2008-01-10 04:28:13 +01:00
|
|
|
$error2 = _t('CreditCardField.VALIDATIONJS2', 'credit card number correctly.');
|
|
|
|
$first = _t('CreditCardField.FIRST', 'first');
|
|
|
|
$second = _t('CreditCardField.SECOND', 'second');
|
|
|
|
$third = _t('CreditCardField.THIRD', 'third');
|
|
|
|
$fourth = _t('CreditCardField.FOURTH', 'fourth');
|
2007-07-19 12:40:28 +02:00
|
|
|
$jsFunc =<<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
"#$formID": {
|
|
|
|
validateCreditCard: function(fieldName) {
|
|
|
|
if(!$(fieldName + "_Holder")) return true;
|
|
|
|
|
|
|
|
// Creditcards are split into multiple values, so get the inputs from the form.
|
|
|
|
var cardParts = $(fieldName + "_Holder").getElementsByTagName('input');
|
|
|
|
|
|
|
|
var cardisnull = true;
|
|
|
|
var i=0;
|
|
|
|
|
|
|
|
for(i=0; i < cardParts.length ; i++ ){
|
|
|
|
if(cardParts[i].value == null || cardParts[i].value == "")
|
|
|
|
cardisnull = cardisnull && true;
|
|
|
|
else
|
|
|
|
cardisnull = false;
|
|
|
|
}
|
|
|
|
if(!cardisnull){
|
|
|
|
// Concatenate the string values from the parts of the input.
|
|
|
|
for(i=0; i < cardParts.length ; i++ ){
|
|
|
|
// The creditcard number cannot be null, nor have less than 4 digits.
|
|
|
|
if(
|
|
|
|
cardParts[i].value == null || cardParts[i].value == "" ||
|
|
|
|
cardParts[i].value.length < 3 ||
|
|
|
|
!cardParts[i].value.match(/[0-9]{4}/)
|
|
|
|
){
|
|
|
|
switch(i){
|
2008-01-10 04:28:13 +01:00
|
|
|
case 0: number = "$first"; break;
|
|
|
|
case 1: number = "$second"; break;
|
|
|
|
case 2: number = "$third"; break;
|
|
|
|
case 3: number = "$fourth"; break;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-01-10 04:28:13 +01:00
|
|
|
validationError(cardParts[i],"$error1 " + number + " $error2","validation",false);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
|
|
|
Requirements :: customScript($jsFunc, 'func_validateCreditCard');
|
|
|
|
|
|
|
|
return "\$('$formID').validateCreditCard('$this->name');";
|
|
|
|
}
|
|
|
|
|
|
|
|
function validate($validator){
|
2007-11-19 04:11:38 +01:00
|
|
|
// If the field is empty then don't return an invalidation message
|
|
|
|
if(!trim(implode("", $this->value))) return true;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$i=0;
|
|
|
|
if($this->value) foreach($this->value as $part){
|
2012-02-27 22:14:02 +01:00
|
|
|
if(!$part || !(strlen($part) == 4) || !preg_match("/([0-9]{4})/", $part)){
|
2007-07-19 12:40:28 +02:00
|
|
|
switch($i){
|
2008-01-10 04:28:13 +01:00
|
|
|
case 0: $number = _t('CreditCardField.FIRST', 'first'); break;
|
|
|
|
case 1: $number = _t('CreditCardField.SECOND', 'second'); break;
|
|
|
|
case 2: $number = _t('CreditCardField.THIRD', 'third'); break;
|
|
|
|
case 3: $number = _t('CreditCardField.FOURTH', 'fourth'); break;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-10-25 04:47:45 +02:00
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
|
|
|
sprintf(
|
|
|
|
_t('Form.VALIDATIONCREDITNUMBER', "Please ensure you have entered the %s credit card number correctly."),
|
|
|
|
$number
|
|
|
|
),
|
|
|
|
"validation",
|
|
|
|
false
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|