/    Sign up×
Community /Pin to ProfileBookmark

comma deilimited number

i want, when i type a number in text box to format the number,
so i will get :
12345678 ==>12,345,678
i tried to use this code : [url]http://blog.stevenlevithan.com/archives/commafy-numbers[/url]
to format number when using the keyup event.
(i tried to use code from other sources too but i get the same problem):
when i copy paste the number the code works great and the number is formated as excpected,
but when i try i get : 1,234,5,678

what am i missing?
this is one of my code test :

[CODE]
function FixNumberRepresentation(obj, val) {
var str = val;
str = str.replace(“,”, “”);
alert(parseInt(str).commafy());
str = str.replace(/(d)(?=(ddd)+(?!d))/g, “$1,”);
obj.value = str;
return false;
}
NET CODE :
<asp:TextBox runat=”server” ID=”PrepaidStatus_action” Width=”80px” MaxLength=”11″
onkeyup=”return FixNumberRepresentation(this,this.value)” />
<ajaxToolkit:FilteredTextBoxExtender ID=”FilteredTextBoxExtender_PrepaidStatus_action”
runat=”server” TargetControlID=”PrepaidStatus_action” FilterType=”Custom,Numbers”
ValidChars=”,” Enabled=”True” />

[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@007JulienSep 01.2013 — The onkeyup event can't be called many times with this function !

After the first change, it is necessary to remove at first the commas. Change the function to remove it at first with a str=val.replace(/,/g,'').
Copy linkTweet thisAlerts:
@wanttolearn1authorSep 01.2013 — i do :
[CODE]str = str.replace(",", "");[/CODE]
Copy linkTweet thisAlerts:
@007JulienSep 01.2013 — Sorry ! You are right.

I do not understand the proposed asp code with a run at server ?

Could you please post the result HTML page (or an extract).

Then I proposed (See this page)
[CODE]str = str.replace(/(d)(?=(d{3})+b)/g,'$1,');[/CODE]
the b is a word boundary which works with the end of the string and other commas, points, spaces... etc.

It's to possible to call the function on the client side with a onblur event...
Copy linkTweet thisAlerts:
@mrhooSep 06.2013 — You can use the comma insertion method here, but it is annoying to insert things while the user is typing.

Better to use it once- onchange, or just before submitting, whenever you validate the input.


[CODE]<!doctype html>
<html lang="en">
<head>
<meta charset= "utf-8">
<title>Comma Input</title>
<style>
label, input, button{font-size:1.25em}
</style>

<script>

// insert commas as thousands separators
function addCommas(n){
var rx= /(d+)(d{3})/;
return String(n).replace(/^d+/, function(w){
while(rx.test(w)){
w= w.replace(rx, '$1,$2');
}
return w;
});
}

// return integers and decimal numbers from input
function validDigits(n){
var n2= n.replace(/[^d.]+/g, '');
var ax2= n2.lastIndexOf('.');
if(ax2> n2.indexOf('.')) n2= n2.substring(0, ax2);
return n2;
}
window.onload= function(){
var inp= document.getElementById('number1');
inp.value= '';

inp.onkeyup= inp.onchange= function(e){
e=e|| window.event;
var who=e.target || e.srcElement,
temp= validDigits(who.value);
who.value= addCommas(temp);
}
}
</script>

</head>
<body>
<h1>Input Commas</h1>
<div>
<p>
<label> Any number <input id="number1" value="" size="30"></label>
</p></div>
</body>
</html>[/CODE]
×

Success!

Help @wanttolearn1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...