1 <?php
2 3 4 5 6
7 class CreditCardField extends TextField {
8
9 function Field() {
10 $parts = explode("\n", chunk_split($this->value,4,"\n"));
11 $parts = array_pad($parts, 4, "");
12 $field = "<span id=\"{$this->name}_Holder\" class=\"creditCardField\">" .
13 "<input autocomplete=\"off\" name=\"{$this->name}[0]\" value=\"$parts[0]\" maxlength=\"4\"" . $this->getTabIndexHTML(0) . " /> - " .
14 "<input autocomplete=\"off\" name=\"{$this->name}[1]\" value=\"$parts[1]\" maxlength=\"4\"" . $this->getTabIndexHTML(1) . " /> - " .
15 "<input autocomplete=\"off\" name=\"{$this->name}[2]\" value=\"$parts[2]\" maxlength=\"4\"" . $this->getTabIndexHTML(2) . " /> - " .
16 "<input autocomplete=\"off\" name=\"{$this->name}[3]\" value=\"$parts[3]\" maxlength=\"4\"" . $this->getTabIndexHTML(3) . " /></span>";
17 return $field;
18 }
19 function dataValue() {
20 if(is_array($this->value)) return implode("", $this->value);
21 else return $this->value;
22 }
23
24 function jsValidation() {
25 $formID = $this->form->FormName();
26 $error1 = _t('CreditCardField.VALIDATIONJS1', 'Please ensure you have entered the');
27 $error2 = _t('CreditCardField.VALIDATIONJS2', 'credit card number correctly.');
28 $first = _t('CreditCardField.FIRST', 'first');
29 $second = _t('CreditCardField.SECOND', 'second');
30 $third = _t('CreditCardField.THIRD', 'third');
31 $fourth = _t('CreditCardField.FOURTH', 'fourth');
32 $jsFunc =<<<JS
33 Behaviour.register({
34 "#$formID": {
35 validateCreditCard: function(fieldName) {
36 if(!$(fieldName + "_Holder")) return true;
37
38 // Creditcards are split into multiple values, so get the inputs from the form.
39 var cardParts = $(fieldName + "_Holder").getElementsByTagName('input');
40
41 var cardisnull = true;
42 var i=0;
43
44 for(i=0; i < cardParts.length ; i++ ){
45 if(cardParts[i].value == null || cardParts[i].value == "")
46 cardisnull = cardisnull && true;
47 else
48 cardisnull = false;
49 }
50 if(!cardisnull){
51 // Concatenate the string values from the parts of the input.
52 for(i=0; i < cardParts.length ; i++ ){
53 // The creditcard number cannot be null, nor have less than 4 digits.
54 if(
55 cardParts[i].value == null || cardParts[i].value == "" ||
56 cardParts[i].value.length < 3 ||
57 !cardParts[i].value.match(/[0-9]{4}/)
58 ){
59 switch(i){
60 case 0: number = "$first"; break;
61 case 1: number = "$second"; break;
62 case 2: number = "$third"; break;
63 case 3: number = "$fourth"; break;
64 }
65 validationError(cardParts[i],"$error1 " + number + " $error2","validation",false);
66 return false;
67 }
68 }
69 }
70 return true;
71 }
72 }
73 });
74 JS;
75 Requirements :: customScript($jsFunc, 'func_validateCreditCard');
76
77 return "\$('$formID').validateCreditCard('$this->name');";
78 }
79
80 function validate($validator){
81
82 if(!trim(implode("", $this->value))) return true;
83
84 $i=0;
85 if($this->value) foreach($this->value as $part){
86 if(!$part || !(strlen($part) == 4) || !ereg("([0-9]{4})",$part)){
87 switch($i){
88 case 0: $number = _t('CreditCardField.FIRST', 'first'); break;
89 case 1: $number = _t('CreditCardField.SECOND', 'second'); break;
90 case 2: $number = _t('CreditCardField.THIRD', 'third'); break;
91 case 3: $number = _t('CreditCardField.FOURTH', 'fourth'); break;
92 }
93 $validator->validationError(
94 $this->name,
95 sprintf(
96 _t('Form.VALIDATIONCREDITNUMBER', "Please ensure you have entered the %s credit card number correctly."),
97 $number
98 ),
99 "validation",
100 false
101 );
102 return false;
103 }
104 $i++;
105 }
106 }
107 }
108
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-