JavaScript parseMoney

March 31st, 2005

In order to change the input number format to currency format, e.g. 12500 => 12,500. The javascript code I write as follows:

function parseMoney(objID) {
  var obj = document.all[objID]; //get the object
  if(obj.value=='') {
    return; //do no job when the value is empty
  }
  //check number format
  var chkstr = replaceAll(obj.value, ',', '');
  if(chkstr!='' && isNaN(chkstr)) {
    alert('Please enter the right number format!');
    obj.focus(); //focus the object let user remodify
    return;
  }
  //check negative format
  var negative = false;
  if(chkstr.indexOf('-')>=0) {
    negative = true;
    chkstr = chkstr.replace('-', '');
    //omit negative character in advance
    //to prevent -0
  }
  var mArray = chkstr.split('.');
  if(mArray[0]=='') { //in case .xx
    mArray[0] = '0';
  }
  obj.value = parseInt(mArray[0], 10);
  //give back the negative char
  var money = (negative) ? ('-' + obj.value) : obj.value;
  var retstr = '';
  for(var i=money.length-1; i>=0; i--) {
    retstr += money.charAt(money.length-1-i);
    //add comma if it is thousands digit
    //remember to leave out negative char
    if(i%3==0 && i>0 && retstr!='-') {
      retstr += ',';
    }
  }
  if(mArray.length>1) {
     //decimal point exist
    retstr += '.';
    var decimal = '';
    var flag = false;
    for(var i=mArray[1].length-1; i>=0; i--) {
      if(!flag && mArray[1].charAt(i)=='0') {
        continue;
      }
      else if(!flag) {
        flag = true;
      }
      decimal = mArray[1].charAt(i) + decimal;
    }
    if(decimal=='') { //in case x.000
      decimal = '0';
    }
    retstr += decimal;
  }
  if(parseFloat(retstr)=='0') {
    retstr = '0'; //in case 0.0 或 -0.0
  }
  obj.value = retstr;
}
function replaceAll(str, from, to) {
  var idx = str.indexOf(from);
  while(idx>-1) {
    str = str.replace(from, to);
    idx = str.indexOf(from);
  }
  return str;
}

See the test page

JavaScript Trim

March 27th, 2005

JavaScript does not support trim function which VBScript does. Here is the implementation of trim function in JavaScript simply using regular expression.

function trim(str) {
   return str.replace(/^\s*|\s*$/g,\"\");
}

I’m Back

March 26th, 2005

Hello my blog. I am back. Sorry have leaved you aside for a long time. I am quite busy lately and do not have much time to be online.

It seems that I have missed a lot of updated information. Wordpress 1.5 has been released. New Kubrik 1.2.6 template also has come. What else? Friendster’s blog? I have got it here. College essays

How Nerdy Are You?

January 12th, 2005

I am nerdier than 71% of all people. Are you nerdier? Click here to find out!

Upgrade Hotmail to 250MB

January 10th, 2005

Hotmail is my first email account provider and still my primary email until now. But the free spaces given are still 2MB. Very little compared to other free email account providers such as Yahoo! and GMail. I found the way to upgrade Hotmail account to 250MB for free here. custom writing

  • First go to your Hotmail account and log in
  • Then go to the settings of your account and click on “My Profile”
  • You need the adjust the country to United States and the state to Florida (postal code 33332)
  • Then go to the link
    http://by17fd.bay17.hotmail.msn.com/cgi-bin/Accountclose
  • Click on “Close My Account”
  • Go to hotmail.com and log in again
  • Normally you should have a 25MB account now. Soon it will be upgraded to 250MB!

N.B. Try it at your own risk!

Hotmail has promised to upgrade the spaces since June 2004, but most Hotmail users today still have only 2MB spaces. A little disappointed to the service that took a very long time to upgrade globally.

Guess Song

January 9th, 2005

Chinese Song Mania? See how many rounds can you guess the songs.

Good Fonts

January 8th, 2005

Good Fonts for Designers. 300 free truetype fonts collection. (via Jan’s Tech)

Campaign For Real Beauty

January 5th, 2005

Campaign For Real Beauty. See the beauty of a 96-aged model.

ICQ2Go in Flash

January 4th, 2005

ICQ2Go, Web based ICQ changed to Flash based. Faster? Obviously it looks more attractive than the old java applet based.

I Cried For My Brother Six Times

December 29th, 2004

Aku dilahirkan di sebuah dusun pegunungan yang sangat terpencil. Hari demi hari, orang tuaku membajak tanah kering kuning, dan punggung mereka menghadap ke langit. Aku mempunyai seorang adik, tiga tahun lebih muda dariku.

Suatu ketika, untuk membeli sebuah sapu tangan yang mana semua gadis di sekelilingku kelihatannya membawanya. Aku mencuri lima puluh sen dari laci ayahku. Ayah segera menyadarinya. Beliau membuat adikku dan aku berlutut di depan tembok, dengan sebuah tongkat bambu di tangannya.

Read the rest of this entry »