Javascript-Add commas into your given number after every three digits.

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.    <head>  
  4.       <title>Sample Code</title>  
  5.       <script type="text/javascript">  
  6.          function AddComma(MyString)           
  7.          {           
  8.                       var objRegex  = new RegExp('(-?[0-9]+)([0-9]{3})');                        
  9.                       //Check For Criteria....           
  10.                       while(objRegex.test(MyString))           
  11.                       {           
  12.                                   //Add Commas After Every Three Digits Of Number...           
  13.                                   MyString = MyString.replace(objRegex, '$1,$2');           
  14.                       }           
  15.                       alert(MyString);           
  16.          }           
  17.       </script>  
  18.    </head>  
  19.    <body bgcolor=#00ff00 alink=blue vlink=blue>  
  20.       <FORM name="windowEvent">  
  21.          Enter String : <input type="text" name="txtOriginal" /><br>  
  22.          <input type="button" value="Remove Comma" name="btnAddComma" onClick="AddComma(txtOriginal.value);" />  
  23.       </FORM>  
  24.    </body>  
  25. </html>