net.sf.click.extras.control
Class CreditCardField

java.lang.Object
  extended bynet.sf.click.control.AbstractControl
      extended bynet.sf.click.control.Field
          extended bynet.sf.click.control.TextField
              extended bynet.sf.click.extras.control.CreditCardField
All Implemented Interfaces:
Control, Serializable

public class CreditCardField
extends TextField

Provides a Credit Card control:   <input type='text'><select>.

Credit Card
CreditCardField will validate the card number against the selected card type when the control is processed.

Supported card include VISA, MASTER, AMEX, DINERS and DISCOVER.

The CreditCardField uses a JavaScript onkeypress() integerFilter() method to prevent users from entering invalid characters. To enable number key filtering reference the PageImports object in the page template.

CreditCardField Example

A CreditCardField example is provided below:
 <html>
 <head>
 $cssImports
 </head>
 <body>
 $form
 </body>
 </html>
 $jsImports 
An example page using CreditCardField is provided below:
 public class PaymentPage extends Page {

     private Form form = new Form("form");
     private CreditCardField creditCardField  = new CreditCardField("creditCard", true);
     private IntegerField expiryField  = new IntegerField("expiryDate", true);

     public PaymentPage() {
         form.add(creditCardField);

         expiryField.setMinLength(4);
         expiryField.setMaxLength(4);
         expiryField.setSize(4);
         form.add(expiryField);

         form.add(new Submit("ok", "  OK  "));
         form.add(new Submit("cancel", this, "onCancelClick"));

         addControl(form);
     }

     public boolean onCancelClick() {
         setRedirect("index.htm");
         return false;
     }

     public void onPost() {
         if (form.isValid()) {
             String cardType = creditCardField.getCardType();
             Long cardNumber = creditCardField.getCardNumber();
             String expiryDate = expiryField.getInteger();

             // Make payment
             ..
         }
     }
 } 
See also W3C HTML reference INPUT

Author:
Malcolm Edgar
See Also:
Serialized Form

Field Summary
static String AMEX
          The American Express type credit card: "AMEX".
protected static List CARD_OPTIONS
          The statically initialized card type options list.
protected  String cardType
          The type of credit card: ["VISA" | "MASTER" | "AMEX" | "DINERS" | "DISCOVER"].
protected  Select cardTypeSelect
          The card type Select.
static String DINERS
          The Diners Club type credit card: "DINERS".
static String DISCOVER
          The Discovery type credit card: "DISCOVER".
static String HTML_IMPORTS
          The CreditCardField imports statement.
static String MASTER
          The MasterCard type credit card: "MASTER".
protected static String SELECT_NAME
          The card type Select name.
protected static String VALIDATE_CREDITCARD_FUNCTION
          The field validation JavaScript function template.
static String VISA
          The Visa type credit card: "VISA".
 
Fields inherited from class net.sf.click.control.TextField
maxLength, minLength, size, VALIDATE_TEXTFIELD_FUNCTION
 
Fields inherited from class net.sf.click.control.Field
disabled, error, focus, form, help, label, readonly, required, tabindex, title, validate, value
 
Fields inherited from class net.sf.click.control.AbstractControl
actionListener, attributes, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface net.sf.click.Control
CONTROL_MESSAGES
 
Constructor Summary
CreditCardField()
          Create a credit card field with no name defined.
CreditCardField(String name)
          Construct the credit card field with the given name.
CreditCardField(String name, boolean required)
          Construct the credit card field with the given name and required status.
CreditCardField(String name, String label)
          Construct the credit card field with the given name and label.
CreditCardField(String name, String label, boolean required)
          Construct the credit card field with the given name, label and required status.
 
Method Summary
 void bindRequestValue()
          Bind the request submission, setting the value property and cardType property if defined in the request.
 Long getCardNumber()
          Return the Credit Card number.
 String getCardType()
          Return the selected Credit Card type:   [ "VISA" | "MASTER" | "AMEX" | "DINERS" | "DISCOVER" ].
 int getControlSizeEst()
           
 String getHtmlImports()
          Return the HTML head import statements for the CreditCardField.js.
 String getValidationJavaScript()
          Return the field JavaScript client side validation function.
 void onDeploy(ServletContext servletContext)
          Deploy the extras-control.js file to the click web directory when the application is initialized.
 void render(HtmlStringBuffer buffer)
          Render the HTML representation of the CreditCardField.
 void validate()
          Validate the CreditCardField request submission, using the card type to validate the card number.
 
Methods inherited from class net.sf.click.control.TextField
getMaxLength, getMinLength, getSize, getTag, getType, setMaxLength, setMinLength, setSize
 
Methods inherited from class net.sf.click.control.Field
getError, getErrorLabel, getFocus, getFocusJavaScript, getForm, getHelp, getId, getLabel, getRequestValue, getTabIndex, getTextAlign, getTitle, getValidate, getValue, getValueObject, getWidth, isDisabled, isHidden, isReadonly, isRequired, isValid, onInit, onProcess, setDisabled, setError, setErrorMessage, setErrorMessage, setErrorMessage, setErrorMessage, setFocus, setForm, setHelp, setLabel, setListener, setParent, setReadonly, setRequired, setTabIndex, setTextAlign, setTitle, setValidate, setValue, setValueObject, setWidth
 
Methods inherited from class net.sf.click.control.AbstractControl
addStyleClass, appendAttributes, getActionListener, getAttribute, getAttributes, getContext, getMessage, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, hasAttribute, hasAttributes, hasStyles, onDestroy, onRender, registerActionEvent, removeStyleClass, renderTagBegin, renderTagEnd, setActionListener, setAttribute, setId, setName, setStyle, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VISA

public static final String VISA
The Visa type credit card: "VISA".

See Also:
Constant Field Values

MASTER

public static final String MASTER
The MasterCard type credit card: "MASTER".

See Also:
Constant Field Values

AMEX

public static final String AMEX
The American Express type credit card: "AMEX".

See Also:
Constant Field Values

DINERS

public static final String DINERS
The Diners Club type credit card: "DINERS".

See Also:
Constant Field Values

DISCOVER

public static final String DISCOVER
The Discovery type credit card: "DISCOVER".

See Also:
Constant Field Values

CARD_OPTIONS

protected static final List CARD_OPTIONS
The statically initialized card type options list.


SELECT_NAME

protected static final String SELECT_NAME
The card type Select name.

See Also:
Constant Field Values

VALIDATE_CREDITCARD_FUNCTION

protected static final String VALIDATE_CREDITCARD_FUNCTION
The field validation JavaScript function template. The function template arguments are:

See Also:
Constant Field Values

HTML_IMPORTS

public static final String HTML_IMPORTS
The CreditCardField imports statement.

See Also:
Constant Field Values

cardType

protected String cardType
The type of credit card: ["VISA" | "MASTER" | "AMEX" | "DINERS" | "DISCOVER"]. The default value is "VISA"


cardTypeSelect

protected Select cardTypeSelect
The card type Select.

Constructor Detail

CreditCardField

public CreditCardField(String name)
Construct the credit card field with the given name.

Parameters:
name - the name of the field

CreditCardField

public CreditCardField(String name,
                       String label)
Construct the credit card field with the given name and label.

Parameters:
name - the name of the field
label - the label of the field

CreditCardField

public CreditCardField(String name,
                       boolean required)
Construct the credit card field with the given name and required status.

Parameters:
name - the name of the field
required - the field required status

CreditCardField

public CreditCardField(String name,
                       String label,
                       boolean required)
Construct the credit card field with the given name, label and required status.

Parameters:
name - the name of the field
label - the label of the field
required - the field required status

CreditCardField

public CreditCardField()
Create a credit card field with no name defined.

Please note the control's name must be defined before it is valid.

Method Detail

getCardType

public String getCardType()
Return the selected Credit Card type:   [ "VISA" | "MASTER" | "AMEX" | "DINERS" | "DISCOVER" ].

Returns:
the selected Credit Card type

getCardNumber

public Long getCardNumber()
Return the Credit Card number.

Returns:
the Credit Card number

getHtmlImports

public String getHtmlImports()
Return the HTML head import statements for the CreditCardField.js.

Returns:
the HTML head import statements for the CreditCardField.js

bindRequestValue

public void bindRequestValue()
Bind the request submission, setting the value property and cardType property if defined in the request.


getControlSizeEst

public int getControlSizeEst()
Returns:
the estimated rendered control size in characters
See Also:
AbstractControl.getControlSizeEst()

render

public void render(HtmlStringBuffer buffer)
Render the HTML representation of the CreditCardField.

Parameters:
buffer - the specified buffer to render the control's output to
See Also:
AbstractControl.toString()

getValidationJavaScript

public String getValidationJavaScript()
Return the field JavaScript client side validation function.

The function name must follow the format validate_[id], where the id is the DOM element id of the fields focusable HTML element, to ensure the function has a unique name.

Returns:
the field JavaScript client side validation function

validate

public void validate()
Validate the CreditCardField request submission, using the card type to validate the card number.

A field error message is displayed if a validation error occurs. These messages are defined in the resource bundle:

  • /click-control.properties
    • field-maxlenght-error
    • field-minlength-error
    • field-required-error
  • /net/sf/click/extras/control/CreditCardField.properties
    • creditcard-number-error


onDeploy

public void onDeploy(ServletContext servletContext)
Deploy the extras-control.js file to the click web directory when the application is initialized.

Parameters:
servletContext - the servlet context
See Also:
Control.onDeploy(ServletContext)