Since the input fields are hosted on another page, displayed in an iframe, it is not possible to use the classic for attribute for labels, due to the Same-Origin Policy. (Strict cross-origin isolation between the parent domain and the iframe's content)

However it is possible to simulate this property in this way:

1. Add a onclick event on the labels

html
Copy
<p>
	<label id="card-label" onclick="focusIframe(\'hosted-fields-frame-card\');">Card number</label>
	<!-- Card number container -->
</p>

<p>
	<label id="expiry-label" onclick="focusIframe(\'hosted-fields-frame-expiry\');">Expiry</label>
	<!-- Card expiry container -->
</p>

<p>
	<label id="cryptogram-label" onclick="focusIframe(\'hosted-fields-frame-cryptogram\');">Cryptogram</label>
	<!-- Card cryptogram container -->
</p>

This onclick event can be added on the containers themself to avoid unclickable dead zones around iframes. <label>

Label Iframe id
card-label hosted-fields-frame-card
expiry-label hosted-fields-frame-expiry
cryptogram-label hosted-fields-frame-cryptogram

Trick

The parameter passed as an argument to the function must refer to the id of the frame corresponding to the

This onclick event can be added on the containers themselves, to avoid unclickable dead zones around the iframes.

js
Copy
function focusIframe(id)
{
    var iframe = document.getElementById(id);
    iframe.contentWindow.focus();
    return true;
}

2. Add this little function in JavaScript <label> This function relays the click on the <input type="text"> to the iframe, whose sole content is the corresponding

.

Custom your form according to the card type

js
Copy
function formCustom(cardType)
{
    // Customize your form as you wish, for example by adding a pictogram of the type of card used
    var picto= document.getElementById("cardPictogram");
    picto.className = cardType;
}

Reminder

1. Create a function to apply a class depending of the card type

  • american_express
  • bancontact
  • maestro
  • mastercard
  • visa

The Dalenys available values for cardType parameter are as follows:

js
Copy
var hfields = be2bill.hostedFields({
    key: {
        id: "Your API Key",
        value: "Your API Key value"
    },
    fields: {
        'card': {
            id: 'card-container',
            onInput: function(event) {
                if(event.cardType) {
                    formCustom(event['cardType']);
                }
            }
        }

2. Call this fonction for the input event of the card input field

css
Copy
#cardPictogram.mastercard {
    background-image: url("mastercard-logo.svg");
}
...

3. Manage your classes in your CSS

Imitate Material Design floating label effect

As seen previously, the input fields being hosted on an external page, outside the DOM, it is inconceivable to consider animating their placeholder attributes.

So you have to fake it, making labels look like placeholders.

html
Copy
<p>
    <label id="card-label" onclick="focusIframe(\'hosted-fields-frame-card\');">Card number</label>
    <span class="input-container" id="card-container" onclick="focusIframe(\'hosted-fields-frame-card\');"></span>
</p>

<p>
    <label id="card-label" onclick="focusIframe(\'hosted-fields-frame-expiry\');">Expiry</label>
    <span class="input-container" id="expiry-container" onclick="focusIframe(\'hosted-fields-frame-expiry\');"></span>
</p>

<p>
    <label id="card-label" onclick="focusIframe(\'hosted-fields-frame-cryptogram\');">Cryptogram</label>
    <span class="input-container" id="cryptogram-container" onclick="focusIframe(\'hosted-fields-frame-cryptogram\');"></span>
</p>
css
Copy
form p {
    /* The position of each paragraph will be used as a reference to
     * set the absolute position of the child elements <label> */
    position: relative;
    height: 40px;
}

label {
    /* Gives an absolute position based on the parent elements <p> */
    z-index: 1;
    position: absolute;
    padding: 10px 7px;
    left: 0;
    bottom: 0;

    /* Labels must be superimposed on the input fields */
    display: block;
    width: 100%;
    height: 40px;
    box-sizing: border-box;

    /* Simulates a placeholder style */
    fonts-style: italic;
    color: #bbb;

    /* Simulates an on hover input text cursor */
    cursor: text;

    /* Smooth transition effect */
    transition: all 250ms ease-in-out;
}

1. To get started, you have to display floating labels over the input fields:

2. One might be tempted to start the labels animation only when clicking on them, but the user could use the TAB key to focus to target the iframes.

css
Copy
// Detects the use of any key, especially the TAB key, to focus on a hosted field.
document.onkeydown = function(){
    formDynamics();
};

// Detects the use of the mouse
document.onclick = function(){
    formDynamics();
};

So add onclick and onkeydown listeners to make sure you don't miss any command to target a hosted field:

3. In case a listener is triggered, hold on 100 ms before checking if a container class has changed, and indicates a focus on a hosted field. <label> If so, add classes on all the labels to trigger their animations:

js
Copy
function formDynamics() {
    var cardField =         document.getElementById("card-container");
    var cardLabel =         document.getElementById("card-label");
    var expiryField =       document.getElementById("expiry-container");
    var expiryLabel =       document.getElementById("expiry-label");
    var cryptogramField =   document.getElementById("cryptogram-container");
    var cryptogramLabel =   document.getElementById("cryptogram-label");

    // Hold on 100ms before checking for the presence of a "focus" class on the hosted-fields containers
    setTimeout(function() {

        // If the focus is confirmed on any hosted-field...
        if( cardField.className.indexOf("hosted-fields-focus-state") > -1
            ||  expiryField.className.indexOf("hosted-fields-focus-state") > -1
            ||  cryptogramField.className.indexOf("hosted-fields-focus-state") > -1
        ) {

            // ...Then add classes on the labels to trigger their animations
            cardLabel.classList.add("up");
            expiryLabel.classList.add("up");
            cryptogramLabel.classList.add("up");
        }
    }, 100);
}

4. Manage the CSS animations:

css
Copy
label.up {
    padding: 0;
    bottom: 42px;
    height: 20px;
    fonts-size: 75%;
}

Animation of an input field with incorrect data

Just manage the .hosted-fields-invalid-state CSS class:

css
Copy
.hosted-fields-invalid-state {
    border: 2px solid red;
    animation-name: wriggle;
    animation-duration: 75ms;
    animation-iteration-count: 2;
    animation-direction: normal;
    animation-timing-function: linear;
}

And create a shaking animation:

css
Copy
@keyframes wriggle {
    from { transform: translateY(-3px); }
    to { transform: translateY(0); }
}

Practical example

Warning

The following payment form demo is hosted on a preproduction environment; no actual bank movements will occur.

However, as I'm not familiar with the privacy policy for banking information submitted on Codepen, I strongly advise against using real bank card data; use one of these instead:

  • VISA: 4111 1111 1111 1111
  • MasterCard: 5111 1111 1111 1118
  • American Express: 3444 444444 44447
  • Bancontact: 67777777777777775

Any expiration date and any 3-digit security code will be accepted.


Comments

Join the discussion?

Please, let us take care with our writing, and let us be respectful and constructive.