Created
June 28, 2012 21:41
-
-
Save davidvanvickle/3014111 to your computer and use it in GitHub Desktop.
JS - put commas in dollar amount
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
<title>Add commas</title> | |
</head> | |
<body> | |
<script type="text/javascript" charset="utf-8"> | |
// fyi | |
// ignores numbers with cent amounts != '.00' | |
// removes '.00' | |
// can start with '$' | |
// will add '$' before number longer than 4 characters if '$' not in string | |
// to handle one field, set f_cval.n | |
// to handle multiple fields, add field name to array f_cval.na | |
function add_commas () { | |
var f_cval = {n:"inputtext",na:['mud_1','mud_2','mud_3','mud_4','mud_5','mud_6','mud_7','mud_8','mud_9','mud_10'],nt:'',sa:[],i:0,j:0,k:0,m:0,t:"",tm:"",f:null}; | |
if (f_cval.na.length==0) f_cval.na.push(f_cval.n); | |
for (f_cval.m=0;f_cval.m<f_cval.na.length;f_cval.m++) { | |
f_cval.nt = f_cval.na[f_cval.m]; | |
if (document.Main && document.Main[f_cval.nt] && document.Main[f_cval.nt].value!='') { | |
f_cval.f = document.Main[f_cval.nt]; | |
f_cval.sa = f_cval.f.value.split(' '); | |
for (f_cval.i=0;f_cval.i<f_cval.sa.length;f_cval.i++) { | |
if (f_cval.sa[f_cval.i].length < 4) continue; | |
if (f_cval.sa[f_cval.i].substr(f_cval.sa[f_cval.i].length-3,3)=='.00') { | |
f_cval.sa[f_cval.i] = f_cval.sa[f_cval.i].substr(0,f_cval.sa[f_cval.i].length-3); | |
} | |
if (f_cval.sa[f_cval.i].indexOf('.')!=-1) continue; | |
f_cval.t=''; | |
f_cval.k=0; | |
for (f_cval.j=(f_cval.sa[f_cval.i].length-1);f_cval.j>-1;f_cval.j--) { // each letter, count from the back | |
if (f_cval.j==0 && f_cval.sa[f_cval.i].charAt(f_cval.j)=='$') { f_cval.j--; f_cval.t+='$'; break; } | |
if (isNaN(parseInt(f_cval.sa[f_cval.i].charAt(f_cval.j),10))) break; | |
if (f_cval.k==3) { f_cval.t += ','; f_cval.k=1; } else { f_cval.k++; } | |
f_cval.t += f_cval.sa[f_cval.i].charAt(f_cval.j); | |
} | |
if (f_cval.j==-1) { | |
f_cval.tm = ''; | |
for (f_cval.j=(f_cval.t.length-1);f_cval.j>-1;f_cval.j--) f_cval.tm += f_cval.t.charAt(f_cval.j); | |
if (f_cval.f.value.indexOf('$')==-1) f_cval.sa[f_cval.i] = '$'+f_cval.tm; | |
else f_cval.sa[f_cval.i] = f_cval.tm; | |
} | |
} | |
f_cval.f.value = f_cval.sa.join(' '); | |
} | |
} | |
} | |
</script> | |
<form action="commas_submit" method="get" onsubmit="return false;" accept-charset="utf-8" name="Main"> | |
<label for="inputtext">inputtext</label><input type="text" name="inputtext" value="" id="inputtext"> | |
<label for="mud_1">mud_1</label><input type="text" name="mud_1" value="" id="mud_1"> | |
<label for="mud_2">mud_2</label><input type="text" name="mud_2" value="" id="mud_2"> | |
<p><input type="button" onClick="add_commas();" value="Continue →"></p> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment