How to add comma every 10 characters ?

Hi all, advance thanks.

I want to add commas > , < every 10 characters in a form input text area when typing in this textarea.

Ex : 4584789568,4578545265,2487589564,3589546826,4581256478

Can anyone help me ?

thanks

Made it now in a hurry, i dont really know if it really is that efficient the way i did it now, maybe there are faster methods.
Hope it is good. Cheers
Result: 4584789568,8954682645,1268264581,4581268264,8126826458,2682645812,6478
[php]

<?php $a = "4584789568589546826458126826458164581268264581268264581268264581256478"; $n = strlen($a); $ab = str_split($a); $j=0; for($i = 0; $i<=$n; $i++){ if($j == 10){ $j=-1; array_splice($ab, $i, 1, ","); } $j++; } $z = implode("", $ab); echo $z; ?>

[/php]

Hi zetcoby,
Thank you for replay, Your php script are working, but its get comma only after form/value submit. Actually i was get a javascript code for as my need.

<script type="text/javascript"> function format(input) { var nStr = input.value + ''; nStr = nStr.replace( /\,/g, ""); var x = nStr.split( '.' ); var x1 = x[0]; var x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{10})/; while ( rgx.test(x1) ) { x1 = x1.replace( rgx, '$1' + ',' + '$2' ); } input.value = x1 + x2; } </script> <textarea id="" name="" class="" rows="5" cols="50" onkeyup="format(this)" ></textarea>
It is automatically add comma every 10 digits when we typing in textarea.
Thanks.

Ups, somehow i skipped the part where u say “when typing in this textarea.” :smiley: my bad.
oh and only now i realize that i am in the javascript & ajax category… idk i think i was to drunk last night xD

Sponsor our Newsletter | Privacy Policy | Terms of Service