//
you're reading...
HTML, IT-Center, Javascript, Web Based

input text Currency Format using Javascript

Input text for money currency very important, but that need write and change on key press. This problem can be solve using javascript, let’s read this sample :

 <html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<script language=javascript>

function currencyFormat(fld, milSep, decSep, e) {
		var sep = 0;
		var key = '';
		var i = j = 0;
		var len = len2 = 0;
		var strCheck = '0123456789';
		var aux = aux2 = '';
		var whichCode = (window.Event) ? e.which : e.keyCode;

		if (whichCode == 13 ) return true; // Enter
		if (whichCode == 8 ) return true; // Delete
			key = String.fromCharCode(whichCode); // Get key value from key code
		if (strCheck.indexOf(key) == -1) return false; // Not a valid key
			len = fld.value.length;
		for(i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
			aux = '';
		for(; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
		aux += key;
		len = aux.length;
		if (len == 0) fld.value = '';
		if (len == 1) fld.value = '0'+ decSep + '0' + aux;
		if (len == 2) fld.value = '0'+ decSep + aux;
		if (len > 2) {
			aux2 = '';
			for (j = 0, i = len - 3; i >= 0; i--) {
				if (j == 3) {
					aux2 += milSep;
					j = 0;
				}
				aux2 += aux.charAt(i);
				j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
			fld.value += aux2.charAt(i);
			fld.value += decSep + aux.substr(len - 2, len);
		}
		return false;
}
</script>
</head>
<body>
<input type="text" name="txt_harga" id="txt_harga"  value='' autocomplete='off' onKeyPress="return(currencyFormat(this,',','.',event))">
</body>
</html>

_________________________________________________________________________________
Other Script :

<script>
function format2money(frmObj, ctrlID){
 var tmpCtrl = frmObj(ctrlID);
 var oripos = getPosition(tmpCtrl);
 var oritext = tmpCtrl.value;
 var tmpCtrlX = frmObj((ctrlID + "X"));
 var re;
 re = /\,/g;

 if(tmpCtrl.value.length > 0) {
 var tmpStr="";
 tmpStr = tmpCtrl.value.replace(re, '');
 var negatif = tmpStr.substring(0,1);
 var panjang = tmpStr.length;
 var tmpStrX = tmpCtrlX.value;
 var tmpNegatif = "";
 var dummyStr = "";

 if (negatif == "-") {
 if (panjang < 2 ) {
 tmpStr = "";
 } else {
 dummyStr = tmpStr.substring(1, panjang);
 tmpStr = dummyStr;
 }
 } else {
 if (negatif == "+") {
 if (panjang < 2) {
 tmpStr = "";
 alert("Please insert a number!");
 tmpCtrl.value = "";
 } else {
 tmpNegatif = tmpStr.substring(1, 2);
 if (tmpNegatif == "-") {
 negatif = "-";
 } else {
 negatif = "";
 }
 dummyStr = tmpStr.substring(1, panjang);
 tmpStr = dummyStr;
 }
 } else {
 negatif = "";
 }
 }

 if (tmpStr == tmpStrX) {
 } else {
 if (isNaN(tmpStr)) {
 alert("Please insert a number!");
 if (isNaN(tmpCtrlX.value)) {
 tmpStr = '';
 tmpCtrlX.value = '';
 } else {
 tmpNegatif = tmpStr.substring(1, 2);
 if (tmpNegatif == "-") {
 negatif = "-";
 }
 tmpStr = tmpCtrlX.value;
 }
 } else {
 var negatifX = tmpStr.substring(0,1);
 if (negatifX == "-") {
 alert("Please insert a number!");
 if (isNaN(tmpCtrlX.value)) {
 tmpStr = '';
 tmpCtrlX.value = '';
 } else {
 tmpStr = tmpCtrlX.value;
 }
 } else {
 if (negatifX == "+") {
 alert("Please insert a number!");
 dummyStr = tmpStr.substring(1, panjang - 1);
 tmpStr = dummyStr;
 } else {

 }
 }
 tmpCtrlX.value = tmpStr;
 }

 re = /\./g;
 var epos = tmpStr.search(re);
 var bfrComma = "";
 var aftComma = "";
 if(epos >= 0)
 {
 bfrComma = tmpStr.substring(0,epos);
 aftComma = tmpStr.substr(epos);
 }
 else
 {
 bfrComma = tmpStr;
 }

 var tagComma = 3;
 while(bfrComma.length > tagComma) {
 bfrComma = bfrComma.slice(0,(bfrComma.length-tagComma)) + ',' + bfrComma.slice(-tagComma); // insert Comma ",".
 tagComma = tagComma + 4;
 }
 tmpCtrl.value = negatif+bfrComma+aftComma;
 }
 } else {
 tmpCtrl.value = '';
 tmpCtrlX.value = '';
 }

 if ((oritext.length%4) == 0)
 {
 oripos = oripos +1;
 }

 var test = oritext.substring(0,1);
 var nilai = oritext.substring(1,25);
 if (test =="0")
 {
   }
     else
  {
    setPosition(tmpCtrl,oripos);
  }
}
</script>

How to use that script? Example in ASP.NET :

<asp:textbox id="txtVolume" onkeyup="Javascript:format2money(document.Form1, 'txtVolume');"
 style='text-align:right' runat="server" Width="144px" MaxLength="19" TabIndex="7" Columns="19"></asp:textbox>

About berbagisolusi

Berbagi merupakan sebuah bentuk simbol keikhlasan untuk membantu dan menolong, sedangkan solusi adalah cara menyelesaikan masalah. Setiap manusia pasti mengalami masalah, tetapi kita tidak perlu mengalami masalah yang sama jika orang lain pernah mengalami dan kita tahu hal tersebut.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

December 2011
M T W T F S S
« Nov   Jan »
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

Web Statistic

Blog Stats

  • 149,551 hits
%d bloggers like this: