Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Vue.js Money Format Directive
import Vue from 'vue';
Vue.directive('xmoney', function (el) {
let value = el.value.split(',').join('');
if (isNaN(value)) {
value = el.value.replace(/\D/g, '');
}
let result = '';
let valueArray = value.split('');
let resultArray = [];
let counter = 0;
let temp = '';
for (let i = valueArray.length - 1; i >= 0; i--) {
temp += valueArray[i];
counter++;
if (counter === 3) {
resultArray.push(temp);
counter = 0;
temp = '';
}
}
if (counter > 0) {
resultArray.push(temp);
}
for (let i = resultArray.length - 1; i >= 0; i--) {
let resTemp = resultArray[i].split('');
for (let j = resTemp.length - 1; j >= 0; j--) {
result += resTemp[j];
}
if (i > 0) {
result += ','
}
}
el.value = result;
});
@saeedvaziry

This comment has been minimized.

Copy link
Owner Author

saeedvaziry commented Mar 3, 2019

Vue.js Money Format Directive

Usage

// in your scrips
import 'xmoney';

// in your template
<input type="text" xmoney />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.