cleave angular demo

cleave angular demo

by basecss

HTML

<script src="https://nosir.github.io/cleave.js/dist/cleave-angular.min.js"></script>
<script src="https://nosir.github.io/cleave.js/dist/cleave-phone.i18n.js"></script>
<div ng-app="app" ng-controller="AppController">
    <section>
        <input type="text" placeholder="Enter credit card number" ng-model="model.creditCardRawValue" cleave="options.creditCard" />
        <p>raw (ng-model) value: {{model.creditCardRawValue}}</p>
        <p>formatted value: {{model.creditCardFormattedValue}}</p>
        <p>type: {{model.creditCardType}}</p>
    </section>

    <section>
        <input cleave="options.phone" type="text" placeholder="Enter AU phone number" ng-model="model.phone" />
    </section>

    <section>
        <input cleave="options.date" type="text" placeholder="Enter date: DD/MM/YYYY" ng-model="model.date" />
    </section>

    <section>
        <input cleave="options.numeral" type="text" placeholder="Enter numeral" ng-model="model.numeral" class="input-numeral" />
    </section>

    <section>
        <input cleave="options.custom" type="text" ng-model="model.custom" />
    </section>
</div>

CSS

input {
    width: 80%;
    display: block;
    outline: none;
    -webkit-appearance: none;
    border: 1px solid #e6e6e6;
    border-radius: 5px;
    background-color: #fff;
    line-height: 30px;
    vertical-align: middle;
    height: 33px;
    font-size: 14px;
    padding: 0 8px;
}

.input-numeral {
    text-align: right;
}

.right {
    text-align: right;
}

body {
    font-family: sans-serif;
}

p {
    margin: 0;
    padding: 5px 0 0 0;
    font-size: 14px;
    color: #666;
}

section {
    margin-bottom: 15px;
    width: 50%;
}

.input-numeral {
    text-align: right;
}

JavaScript

angular.module('app', ['cleave.js'])

.controller('AppController', function($scope) {
    $scope.onCreditCardValueChanged = function(e) {
        $scope.model.creditCardFormattedValue = e.target.value;
    };

    $scope.onCreditCardTypeChanged = function(type) {
        $scope.model.creditCardType = type;
    };

    $scope.model = {
        creditCardRawValue: '',
        creditCardFormattedValue: '',
        creditCardType: '',
        phone: '',
        date: '',
        numeral: '',
        custom: 'PREFIX-'
    };

    $scope.options = {
        creditCard: {
            creditCard: true,
            onValueChanged: $scope.onCreditCardValueChanged,
            onCreditCardTypeChanged: $scope.onCreditCardTypeChanged,
            blocks: [2, 3, 5],
            delimiter: 'X'
        },

        phone: {
            phone: true,
            phoneRegionCode: 'AU'
        },

        date: {
            date: true
        },

        numeral: {
            numeral: true
        },

        custom: {
            blocks: [6, 3, 3, 3],
            prefix: 'PREFIX',
            uppercase: true,
            delimiters: ['-', '.']
        }
    };
});