samanyolu
04-30-2007, 10:30 AM
...
delete this, please
delete this, please
|
Click to See Complete Forum and Search --> : el.value.split('').reverse().join('').replace(/(\d{3})(?=\d)/g, '$1,').split('').reve samanyolu 04-30-2007, 10:30 AM ... delete this, please samanyolu 04-30-2007, 11:28 AM Now it is working as I wanted. <script type="text/javascript"> function number() { var el = document.getElementById('inputid'); el.value = el.value.replace(/\D/g,""); el.value = el.value.split('').reverse().join('').replace(/(\d{3})(?=\d)/g, '$1,').split('').reverse().join(''); } </script> <input type="text" value="" onkeyup="number()" id="inputid" > JMRKER 04-30-2007, 12:15 PM Now that you have got this complicated little bit of code working ... What were (are) you trying to do? My guess: 1. Remove all non-digits from textbox field 2. Split each reversed character found in groups of 3 from #1 above Some kind of numbers of encryption? Ultimater 05-02-2007, 01:55 AM After studying it for about 5 minutes it seems to me that it is used to limit user input to the digits 0-9 and it auto inserts a comma after every 3 digits coming from right to left. The two calls to split('').reverse().join('') is simply to get around the weakness in regular repressions since they match from left to right whilest the original poster is trying to match from right to left -- although it would be possible to write a regular expression that can match from right to left if one is clever enough w/o having to first reverse the string. However since your math teacher doesn't add a comma to a three digit number the original poster added a tailing \d nested within a tailing (?=) so that the \d{3} is ONLY matched if followed by a 4th \d (however not considered $2). The reason for purposely not matching the 4th digit is since the regular expression repeats due to the "g" modifier to add more than one comma if necessary and the 2nd loop would start off on the wrong foot whilest trying to count the next 3 digits if one didn't use ?=. JMRKER 05-02-2007, 10:59 AM Thanks "Ultimater" It took me longer than 5 minutes to create my question! Way beyond my regular expression expertise, but interesting to see what it can do. mrhoo 05-02-2007, 12:28 PM If you want to include negative numbers and decimals, the reg exp gets even uglier. This routine inserts commas in any number, or string that converts to a number. function commas(num){ var s= String(num).split(/(?=\.)/); var A=[], n, dec, sign=''; n=s[0]; if(/^\-/.test(n)){ sign= '-'; n= n.substring(1); } dec= s[1] || ''; while(n.length>3){ A.push(n.slice(-3)); n= n.slice(0,-3); } if(n) A.push(n); return sign+A.reverse().join(',')+dec; } webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |