Apache Flex 4.16.0 API Reference | All Packages | All Classes | Index | Frames No Frames |
CreditCardValidator

Package | mx.validators
Class | public class CreditCardValidator
Inheritance | CreditCardValidator Validator flash.events.EventDispatcher

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

The CreditCardValidator class validates that a credit card number is the correct length, has the correct prefix, and passes the Luhn mod10 algorithm for the specified card type. This validator does not check whether the credit card is an actual active credit card account.

You can specify the input to the CreditCardValidator in two ways:

- Use the `cardNumberSource` and `cardNumberProperty` properties to specify the location of the credit card number, and the `cardTypeSource` and `cardTypeProperty` properties to specify the location of the credit card type to validate.
- Use the `source` and `property` properties to specify a single Object. The Object should contain the following fields:
  - `cardType` - Specifies the type of credit card being validated.

    In MXML, use the values: `"American Express"`, `"Diners Club"`, `"Discover"`, `"MasterCard"`, or `"Visa"`.

    In ActionScript, use the static constants `CreditCardValidatorCardType.MASTER_CARD`, `CreditCardValidatorCardType.VISA`, or `CreditCardValidatorCardType.AMERICAN_EXPRESS` `CreditCardValidatorCardType.DISCOVER`, or `CreditCardValidatorCardType.DINERS_CLUB`.

  - `cardNumber` - Specifies the number of the card being validated.

To perform the validation, it uses the following guidelines:

Length:

1. Visa: 13 or 16 digits
2. MasterCard: 16 digits
3. Discover: 16 digits
4. American Express: 15 digits
5. Diners Club: 14 digits or 16 digits if it also functions as MasterCard

Prefix:

1. Visa: 4
2. MasterCard: 51 to 55
3. Discover: 6011
4. American Express: 34 or 37
5. Diners Club: 300 to 305, 36 or 38, 51 to 55

MXML Syntax Show MXML Syntax
Hide MXML Syntax

The `<mx:CreditCardValidator>` tag inherits all of the tag attributes of its superclass, and adds the following tag attributes:

      <mx:CreditCardValidator
        allowedFormatChars=" -"
        cardNumberListener="Object specified by cardNumberSource"
        cardNumberProperty="No default"
        cardNumberSource="No default"
        cardTypeListener="Object specified by cardTypeSource"
        cardTypeProperty="No default"
        cardTypeSource="No default"
        invalidCharError= "Invalid characters in your credit card number. (Enter numbers only.)"
        invalidNumberError="The credit card number is invalid."
        noNumError="No credit card number is specified."
        noTypeError="No credit card type is specified or the type is not valid."
        wrongLengthError="Your credit card number contains the wrong number of digits."
        wrongTypeError="Incorrect card type is specified."
      />

View the examples

See also

mx.validators.CreditCardValidatorCardType

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | allowedFormatChars : String The set of formatting characters allowed in the cardNumber field. | CreditCardValidator
|  | cardNumberListener : IValidatorListener The component that listens for the validation result for the card number subfield. | CreditCardValidator
|  | cardNumberProperty : String Name of the card number property to validate. | CreditCardValidator
|  | cardNumberSource : Object Object that contains the value of the card number field. | CreditCardValidator
|  | cardTypeListener : IValidatorListener The component that listens for the validation result for the card type subfield. | CreditCardValidator
|  | cardTypeProperty : String Name of the card type property to validate. | CreditCardValidator
|  | cardTypeSource : Object Object that contains the value of the card type field. | CreditCardValidator
|  | enabled : Boolean Setting this value to false will stop the validator from performing validation. | Validator
|  | invalidCharError : String Error message when the cardNumber field contains invalid characters. | CreditCardValidator
|  | invalidNumberError : String Error message when the credit card number is invalid. | CreditCardValidator
|  | listener : Object Specifies the validation listener. | Validator
|  | noNumError : String Error message when the cardNumber field is empty. | CreditCardValidator
|  | noTypeError : String Error message when the cardType field is blank. | CreditCardValidator
|  | property : String A String specifying the name of the property of the source object that contains the value to validate. | Validator
|  | required : Boolean If true, specifies that a missing or empty value causes a validation error. | Validator
|  | requiredFieldError : String Error message when a value is missing and the required property is true. | Validator
|  | source : Object Specifies the object containing the property to validate. | Validator
|  | trigger : IEventDispatcher Specifies the component generating the event that triggers the validator. | Validator
|  | triggerEvent : String Specifies the event that triggers the validation. | Validator
|  | wrongLengthError : String Error message when the cardNumber field contains the wrong number of digits for the specified credit card type. | CreditCardValidator
|  | wrongTypeError : String Error message the cardType field contains an invalid credit card type. | CreditCardValidator

Protected Properties
Hide Inherited Protected Properties
Show Inherited Protected Properties

| Property | Defined By
--- | --- | ---
|  | actualListeners : Array [read-only] Contains an Array of listener objects, if any, or the source object. | Validator
|  | actualTrigger : IEventDispatcher [read-only] Contains the trigger object, if any, or the source object. | Validator
|  | resourceManager : IResourceManager [read-only] A reference to the object which manages all of the application's localized resources. | Validator
|  | subFields : Array An Array of Strings containing the names for the properties contained in the value Object passed to the validate() method. | Validator

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | CreditCardValidator() Constructor. | CreditCardValidator
|  | initialized(document:Object, id:String):void Called automatically by the MXML compiler when the Validator is created using an MXML tag. | Validator
|  | validate(value:Object = null, suppressEvents:Boolean = false):ValidationResultEvent Performs validation and optionally notifies the listeners of the result. | Validator
|  | validateAll(validators:Array):Array [static] Invokes all the validators in the validators Array. | Validator
|  | validateCreditCard(validator:CreditCardValidator, value:Object, baseField:String):Array [static] Convenience method for calling a validator. | CreditCardValidator

Protected Methods
Show Inherited Protected Methods
Show Inherited Protected Methods

| Method | Defined By
--- | --- | ---
|  | addListenerHandler():void Sets up all of the listeners for the valid and invalid events dispatched from the validator. | Validator
|  | doValidation(value:Object):Array [override] Override of the base class doValidation() method to validate a credit card number. | CreditCardValidator
|  | getValueFromSource():Object Returns the Object to validate. | Validator
|  | handleResults(errorResults:Array):ValidationResultEvent Returns a ValidationResultEvent from the Array of error results. | Validator
|  | isRealValue(value:Object):Boolean Returns true if value is not null. | Validator
|  | removeListenerHandler():void Disconnects all of the listeners for the valid and invalid events dispatched from the validator. | Validator
|  | resourcesChanged():void This method is called when a Validator is constructed, and again whenever the ResourceManager dispatches a "change" Event to indicate that the localized resources have changed in some way. | Validator

Events
Hide Inherited Events
Show Inherited Events

| Event | Summary | Defined By
--- | --- | --- | ---
|  | invalid | Dispatched when validation fails. | Validator
|  | valid | Dispatched when validation succeeds. | Validator

Protected Constants
Hide Inherited Protected Constants
Show Inherited Protected Constants

| Constant | Defined By
--- | --- | ---
|  | DECIMAL_DIGITS : String = 0123456789 [static] A String containing the decimal digits 0 through 9. | Validator
|  | ROMAN_LETTERS : String = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz [static] A string containing the upper- and lower-case letters of the Roman alphabet ("A" through "Z" and "a" through "z"). | Validator

Property Detail

allowedFormatChars | property

`allowedFormatChars:String`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

The set of formatting characters allowed in the `cardNumber` field.

The default value is `" -" (space and dash)`.

Implementation
`public function get allowedFormatChars():String`
`public function set allowedFormatChars(value:String):void`

cardNumberListener | property |

`cardNumberListener:IValidatorListener`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

The component that listens for the validation result for the card number subfield. If none is specified, use the value specified to the `cardNumberSource` property.

Implementation
`public function get cardNumberListener():IValidatorListener`
`public function set cardNumberListener(value:IValidatorListener):void`

cardNumberProperty | property |

`public var cardNumberProperty:String`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Name of the card number property to validate. This attribute is optional, but if you specify the `cardNumberSource` property, you should also set this property.

cardNumberSource | property |

`cardNumberSource:Object`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Object that contains the value of the card number field. If you specify a value for this property, you must also specify a value for the `cardNumberProperty` property. Do not use this property if you set the `source` and `property` properties.

Implementation
`public function get cardNumberSource():Object`
`public function set cardNumberSource(value:Object):void`

cardTypeListener | property |

`cardTypeListener:IValidatorListener`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

The component that listens for the validation result for the card type subfield. If none is specified, then use the value specified to the `cardTypeSource` property.

Implementation
`public function get cardTypeListener():IValidatorListener`
`public function set cardTypeListener(value:IValidatorListener):void`

cardTypeProperty | property |

`public var cardTypeProperty:String`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Name of the card type property to validate. This attribute is optional, but if you specify the `cardTypeSource` property, you should also set this property.

In MXML, valid values are:

- `"American Express"`
- `"Diners Club"`
- `"Discover"`
- `"MasterCard"`
- `"Visa"`

In ActionScript, you can use the following constants to set this property:

`CreditCardValidatorCardType.AMERICAN_EXPRESS`, `CreditCardValidatorCardType.DINERS_CLUB`, `CreditCardValidatorCardType.DISCOVER`, `CreditCardValidatorCardType.MASTER_CARD`, and `CreditCardValidatorCardType.VISA`.

See also

mx.validators.CreditCardValidatorCardType

cardTypeSource | property |

`cardTypeSource:Object`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Object that contains the value of the card type field. If you specify a value for this property, you must also specify a value for the `cardTypeProperty` property. Do not use this property if you set the `source` and `property` properties.

Implementation
`public function get cardTypeSource():Object`
`public function set cardTypeSource(value:Object):void`

invalidCharError | property |

`invalidCharError:String`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Error message when the `cardNumber` field contains invalid characters.

The default value is `"Invalid characters in your credit card number. (Enter numbers only.)"`.

Implementation
`public function get invalidCharError():String`
`public function set invalidCharError(value:String):void`

invalidNumberError | property |

`invalidNumberError:String`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Error message when the credit card number is invalid.

The default value is `"The credit card number is invalid."`.

Implementation
`public function get invalidNumberError():String`
`public function set invalidNumberError(value:String):void`

noNumError | property |

`noNumError:String`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Error message when the `cardNumber` field is empty.

The default value is `"No credit card number is specified."`.

Implementation
`public function get noNumError():String`
`public function set noNumError(value:String):void`

noTypeError | property |

`noTypeError:String`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Error message when the `cardType` field is blank.

The default value is `"No credit card type is specified or the type is not valid."`.

Implementation
`public function get noTypeError():String`
`public function set noTypeError(value:String):void`

wrongLengthError | property |

`wrongLengthError:String`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Error message when the `cardNumber` field contains the wrong number of digits for the specified credit card type.

The default value is `"Your credit card number contains the wrong number of digits."`.

Implementation
`public function get wrongLengthError():String`
`public function set wrongLengthError(value:String):void`

wrongTypeError | property |

`wrongTypeError:String`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Error message the `cardType` field contains an invalid credit card type. You should use the predefined constants for the `cardType` field: `CreditCardValidatorCardType.MASTER_CARD`, `CreditCardValidatorCardType.VISA`, `CreditCardValidatorCardType.AMERICAN_EXPRESS`, `CreditCardValidatorCardType.DISCOVER`, or `CreditCardValidatorCardType.DINERS_CLUB`.

The default value is `"Incorrect card type is specified."`.

Implementation
`public function get wrongTypeError():String`
`public function set wrongTypeError(value:String):void`
Constructor Detail

CreditCardValidator | () | Constructor

`public function CreditCardValidator()`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Constructor.

Method Detail

doValidation | () | method

`override protected function doValidation(value:Object):Array`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Override of the base class `doValidation()` method to validate a credit card number.

You do not call this method directly; Flex calls it as part of performing a validation. If you create a custom Validator class, you must implement this method.

Parameters

| `value:Object` — an Object to validate.

Returns

| `Array` — An Array of ValidationResult objects, with one ValidationResult object for each field examined by the validator.

validateCreditCard | () | method |

`public static function validateCreditCard(validator:CreditCardValidator, value:Object, baseField:String):Array`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Convenience method for calling a validator. Each of the standard Flex validators has a similar convenience method.

Parameters

| `validator:CreditCardValidator` — The CreditCardValidator instance.
| `value:Object` — A field to validate, which must contain the following fields: `cardType` - Specifies the type of credit card being validated. Use the static constants `CreditCardValidatorCardType.MASTER_CARD`, `CreditCardValidatorCardType.VISA`, `CreditCardValidatorCardType.AMERICAN_EXPRESS`, `CreditCardValidatorCardType.DISCOVER`, or `CreditCardValidatorCardType.DINERS_CLUB`. `cardNumber` - Specifies the number of the card being validated.
| `baseField:String` — Text representation of the subfield specified in the value parameter. For example, if the `value` parameter specifies value.date, the `baseField` value is "date".

Returns

| `Array` — An Array of ValidationResult objects, with one ValidationResult object for each field examined by the validator.

See also

mx.validators.ValidationResult
Examples
CreditCardValidatorExample.mxml

    <?xml version="1.0" encoding="utf-8"?>
    <!--

      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

    -->
    <!-- Simple example to demonstrate the CreditCardValidator. -->
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            xmlns:mx="library://ns.adobe.com/flex/mx">

        <fx:Script>
            import mx.controls.Alert;
        </fx:Script>

        <fx:Declarations>
            <!-- Define model for the credit card data. -->
            <fx:Model id="creditcard">
                <card>
                    <cardType>{cardTypeCombo.selectedItem.data}</cardType>
                    <cardNumber>{cardNumberInput.text}</cardNumber>
                </card>
            </fx:Model>

            <mx:CreditCardValidator id="ccV"
                    cardTypeSource="{creditcard}" cardTypeProperty="cardType"
                    cardNumberSource="{creditcard}" cardNumberProperty="cardNumber"
                    trigger="{myButton}" triggerEvent="click"
                    cardTypeListener="{cardTypeCombo}"
                    cardNumberListener="{cardNumberInput}"
                    valid="Alert.show('Validation Succeeded!');"/>
        </fx:Declarations>

        <s:Panel title="CreditCardValidator Example"
                width="75%" height="75%"
                horizontalCenter="0" verticalCenter="0">
            <mx:Form id="creditCardForm" left="10" right="10" top="10" bottom="10">
                <mx:FormItem label="Card Type">
                    <mx:ComboBox id="cardTypeCombo">
                        <mx:dataProvider>
                            <fx:Object label="American Express" data="American Express"/>
                            <fx:Object label="Diners Club" data="Diners Club"/>
                            <fx:Object label="Discover" data="Discover"/>
                            <fx:Object label="MasterCard" data="MasterCard"/>
                            <fx:Object label="Visa" data="Visa"/>
                        </mx:dataProvider>
                    </mx:ComboBox>
                </mx:FormItem>
                <mx:FormItem label="Credit Card Number">
                    <s:TextInput id="cardNumberInput"/>
                </mx:FormItem>
                <mx:FormItem>
                    <s:Button id="myButton" label="Check Credit"/>
                </mx:FormItem>
            </mx:Form>
        </s:Panel>

    </s:Application>

Sun Mar 5 2017, 03:42 PM +11:00