Here is my code in HTML (Angular):
<input ng-value="minLoanRange" tabindex="1" lable-up id="minLoanRange" class="inputMaterial" ng-model="minLoanRange" ng-disabled="activeType" type="number" max-length-handler required value="" ng-keyup="addCommas(minLoanRange)"/>
Here is my code of addCommas:
$scope.addCommas = function(nStr){
nStr+='';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while(rgx.test(x1)){
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
$scope.minLoanRange = x1+x2;
}
I have put an alert just before $scope.minLoanRange = x1+x2; like alert(x1+x2); It is working properly, just not reflecting on screen.
You don't need value="" and ng-value. Because ng-model supports two way binding. if you assign correct value to that model object. its automatically reflect to html.
I have copied the snippet from #lex answer and made some changes
angular.module('app', [])
.controller('controller', function($scope) {
$scope.modelValue = 0;
$scope.commaValue = '';
$scope.addComma = function() {
let nStr = '' + $scope.modelValue.replace(',', '');
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
$scope.modelValue = x1 + x2;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
<div ng-app="app" ng-controller="controller">
<input ng-model="modelValue" ng-keyup="addComma()">
</div>
You need to use two different variables - one for the model value and one for the value. Here's a working snippet to demonstrate how you can accomplish what you're after.
angular.module('app', [])
.controller('controller', function($scope) {
$scope.modelValue = 0;
$scope.commaValue = '';
$scope.addComma = function() {
let nStr = '' + $scope.modelValue.replace(',', '');
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
$scope.commaValue = x1 + x2;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
<div ng-app="app" ng-controller="controller">
<input ng-value="commaValue" ng-model="modelValue" ng-keyup="addComma()">
</div>
Related
I try to convert the number in text box as comma separated values while typing but only the last values are taken.
<script type="text/javascript">
function addCommas(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(^\d{2})|(\d{1,3})(?=\d{1,3}|$)/g;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
</script>
This is the HTML file:
<asp:TextBox ID="txtbudamt" runat="server" CssClass="text_box" Height="22px" Width="140px" onkeyup="this.value=addCommas(this.value);" onkeydown="return (event.keyCode!=13);" AutoComplete="Off" TabIndex="7"></asp:TextBox>
When I enter numbers till 9999 it gives correct output like 9,999
When I go for 10000 it gives 1,0,000
You can simply use toLocaleString for this. It is also supported on most browsers.
function addCommas(nStr) {
return parseFloat(nStr).toLocaleString('en-GB');
}
Here is my counter:
<script type="text/javascript">
$('.count').each(function () {
$(this).prop('Counter',0).animate({
Counter: $(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function (now) {
$(this).text(Math.ceil(now));
}
});
});
</script>
And I thought that adding this might insert a comma after the first digit:
(1234567890).toLocaleString();
This could work but not sure how to merge it into the above:
function addCommas(nStr)
{
nStr += '';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
Any ideas how to edit this?
Here you have a working example
function formatNumber(num) {
const regex = /\B(?=(\d{3})+(?!\d))/g;
return num.toString().replace(regex, ',');
}
console.log(formatNumber(1));
console.log(formatNumber(100));
console.log(formatNumber(1000));
console.log(formatNumber(1000000));
Why not use Intl.NumberFormat?
console.log(new Intl.NumberFormat('en-EN').format(1234567890));
I have variable which is negative numbers after $ sign (actually it shows currency with currency sign). Please tell me how to show minus currency in brackets with currency sign. I mean to say how to change var val=($125,220,328.00)
My code is looks like this
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
function netAmount(){
var net_amount =0;
$('#productList tr:gt(1)').each(function() {
var row_index= $(this).index();
var qty= $('#productList tr:eq('+row_index+') td input[name="quantity"]').val().replace( /[^0-9\.]/g, '' );
var price= $('#productList tr:eq('+row_index+') td input[name="purchase_price"]').val().replace( /[^0-9\.]/g, '' );
net_amount+= +(parseFloat(qty*price).toFixed(2));
$('input[name="net_ammount"]').val('$'+ addCommas(parseFloat(net_amount).toFixed(2)));
});
}
Now i want if net_amount is looks like -123225.32 then it show in input[name="net_ammount"] as ($123,225.32)
Your regexp doesn't match the minus sign, therefore it is not added in the replacement. Change the regexp to this:
var rgx = /(-?\d+)(\d{3})/;
I have currency text box in asp.net
I use :
<script type="text/javascript">
function Comma(Num) { //function to add commas to textboxes
Num += '';
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
x = Num.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1))
x1 = x1.replace(rgx, '$1' + ',' + '$2');
return x1 + x2;
}
</script>
and
<asp:TextBox ID="amountTextBox" runat="server" onkeyup = "javascript:this.value=Comma(this.value);"></asp:TextBox>
it works and hasn't any problem, but I want when user press *, instead of * be written '000' in text box. how to do this work?
I hope this is what you need
<script type="text/javascript">
function Comma(Num) { //function to add commas to textboxes
if (Num.indexOf("*") != -1) {
Num = document.getElementById('amountTextBox').value.substring(0, Num.indexOf("*")) + '000';
}
Num += '';
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
x = Num.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1))
x1 = x1.replace(rgx, '$1' + ',' + '$2');
return x1 + x2;
}
</script>
I am stuck trying to convert a javascript file that ran perfectly in d6 to d7, I have been searching through the info about drupal behaviours but cannot seem to find the correct manner to resolve this.
The following script returns an unexpected token error ), for the closing bracket before (jQuery).
(function($){
var VAT = 0.2;
var QUANTITY_SUFFIX = "field-po-quantity";
var PRICE_SUFFIX = "field-po-price";
var SUBTOTAL_SUFFIX = "field-po-item-st";
var VAT_SUFFIX = "field-po-item-vat";
var TOTAL_SUFFIX = "field-po-item-total";
var quantityFields;
var priceFields;
var fieldsValid = false;
function isNumber(value) {
return !isNaN(parseFloat(value)) &&
isFinite(value);
}
function validValue(value) {
return isNumber(value) &&
parseFloat(value) > 0;
}
function addCommas(nStr) {
nStr += '';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
function decimalise(value) {
if (isNumber(value)) {
var numericValue = parseFloat(value);
return numericValue.toFixed(2);
}
}
function validateFields() {
var fieldsValid = true;
var allFields = new Array();
quantityFields = jQuery('input[id*="' + QUANTITY_SUFFIX + '"]');
priceFields = jQuery('input[id*="' + PRICE_SUFFIX + '"]');
allFields.push.apply(allFields, quantityFields);
allFields.push.apply(allFields, priceFields);
for (i=0; i<allFields.length; i++) {
var field = allFields[i];
var valueString = field.value;
if (!validValue(valueString)) {
field.style.borderStyle="solid";
field.style.border="inset 1px red";
fieldsValid = false;
} else {
field.style.borderStyle="none";
}
}
this.fieldsValid = fieldsValid;
}
jQuery(document).click(function() {
validateFields();
if (fieldsValid) {
var subTotalSum = 0;
var vatSum = 0;
var totalSum = 0;
jQuery('#field-po-items-values tbody tr:not(.content-multiple-removed-row)').each(function(){
var quantityString = jQuery(this).find('input[id*="' + QUANTITY_SUFFIX + '"]').val();
var numericQuantity = decimalise(quantityString);
var priceString = jQuery(this).find('input[id*="' + PRICE_SUFFIX + '"]').val();
var numericPrice = decimalise(priceString);
var subtotal = parseFloat(numericPrice * numericQuantity);
jQuery(this).find('input[id*="' + SUBTOTAL_SUFFIX + '"]').val(addCommas(decimalise(subtotal)));
var vat = subtotal * VAT;
jQuery(this).find('input[id*="' + VAT_SUFFIX + '"]').val(addCommas(decimalise(vat)));
var total = subtotal + vat;
jQuery(this).find('input[id*="' + TOTAL_SUFFIX + '"]').val(addCommas(decimalise(total)));
subTotalSum += subtotal;
vatSum += vat;
totalSum += parseFloat(total);
});
jQuery('input[id*="edit-field-po-subtotal"]').val(addCommas(decimalise(subTotalSum)));
jQuery('input[id*="edit-field-po-vat"]').val(addCommas(decimalise(vatSum)));
jQuery('input[id*="edit-field-po-total"]').val(addCommas(decimalise(totalSum)));
}
}
})(jQuery);
The file is being called from template.php, using drupal_add_js inserted in the top of the file, not under pre-process or anything.
I understand that the jQuery(document).click(function() { might also be causing an issue and may ultimately be the reason for the unexpected token error?
Thank you in advance
I don`t know whether your code is correct but it is highly recommended to use JavaScript in Drupal>7 this way:
(function ($) {
Drupal.behaviors.yourFunction = {
attach: function(context, settings) {
var VAT = 0.2;
var QUANTITY_SUFFIX = "field-po-quantity";
var PRICE_SUFFIX = "field-po-price";
var SUBTOTAL_SUFFIX = "field-po-item-st";
var VAT_SUFFIX = "field-po-item-vat";
var TOTAL_SUFFIX = "field-po-item-total";
var quantityFields;
var priceFields;
var fieldsValid = false;
function isNumber(value) {
return !isNaN(parseFloat(value)) &&
isFinite(value);
}
function validValue(value) {
return isNumber(value) &&
parseFloat(value) > 0;
}
function addCommas(nStr) {
nStr += '';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
function decimalise(value) {
if (isNumber(value)) {
var numericValue = parseFloat(value);
return numericValue.toFixed(2);
}
}
function validateFields() {
var fieldsValid = true;
var allFields = new Array();
quantityFields = $('input[id*="' + QUANTITY_SUFFIX + '"]');
priceFields = $('input[id*="' + PRICE_SUFFIX + '"]');
allFields.push.apply(allFields, quantityFields);
allFields.push.apply(allFields, priceFields);
for (i=0; i<allFields.length; i++) {
var field = allFields[i];
var valueString = field.value;
if (!validValue(valueString)) {
field.style.borderStyle="solid";
field.style.border="inset 1px red";
fieldsValid = false;
} else {
field.style.borderStyle="none";
}
}
this.fieldsValid = fieldsValid;
}
$(document).click(function() {
validateFields();
if (fieldsValid) {
var subTotalSum = 0;
var vatSum = 0;
var totalSum = 0;
$('#field-po-items-values tbody tr:not(.content-multiple-removed-row)').each(function(){
var quantityString = $(this).find('input[id*="' + QUANTITY_SUFFIX + '"]').val();
var numericQuantity = decimalise(quantityString);
var priceString = $(this).find('input[id*="' + PRICE_SUFFIX + '"]').val();
var numericPrice = decimalise(priceString);
var subtotal = parseFloat(numericPrice * numericQuantity);
$(this).find('input[id*="' + SUBTOTAL_SUFFIX + '"]').val(addCommas(decimalise(subtotal)));
var vat = subtotal * VAT;
$(this).find('input[id*="' + VAT_SUFFIX + '"]').val(addCommas(decimalise(vat)));
var total = subtotal + vat;
$(this).find('input[id*="' + TOTAL_SUFFIX + '"]').val(addCommas(decimalise(total)));
subTotalSum += subtotal;
vatSum += vat;
totalSum += parseFloat(total);
});
$('input[id*="edit-field-po-subtotal"]').val(addCommas(decimalise(subTotalSum)));
$('input[id*="edit-field-po-vat"]').val(addCommas(decimalise(vatSum)));
$('input[id*="edit-field-po-total"]').val(addCommas(decimalise(totalSum)));
}
}
}
};
})(jQuery);
More info about Drupal Behaviours:
Drupal behaviors
Theming, jQuery, DRupal Behaviours