473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,405 developers and data experts.

Numeric Text Box Format

progdoctor
I have scripts that can format a HTML input value as a numeric format or money format.. Hope its useful

Expand|Select|Wrap|Line Numbers
  1. function XFormat(arrayCtl)
  2. {
  3.     this.arrCtl = arrayCtl;
  4.     if (typeof XFormat._initialized == 'undefined') 
  5.     {
  6.         XFormat.prototype.setKeyPress = function(){return 0;};
  7.         XFormat.prototype.setFocus = function(){this.select();};
  8.         XFormat.prototype.setBlur = function(){return 0;};
  9.         XFormat.prototype.setFormat = function (){return 0;}; 
  10.  
  11.         XFormat._initialized = true;
  12.     }
  13. }
  14.  
  15. function XFormatNumber(arrayCtl)
  16. {
  17.     XFormat.call(this,arrayCtl);
  18.     if (typeof XFormatNumber._initialized == 'undefined') 
  19.     {
  20.         XFormatNumber.prototype.setKeyPress = function()
  21.         {
  22.             //mengizinkan karakter '-'(minus),'0-9'(numerik)
  23.             if(event.keyCode==45)
  24.                 return true;
  25.             else if(event.keyCode>=48 && event.keyCode<=57)
  26.                 return true;
  27.             else
  28.                 return false;
  29.         };
  30.  
  31.         XFormatNumber.prototype.setBlur = function()
  32.         {
  33.             if(this.value=='' || isNaN(this.value))
  34.                 this.value = 0;
  35.         };
  36.  
  37.         XFormatNumber.prototype.setFormat = function () 
  38.         {
  39.             if(this.arrCtl.length>0)
  40.             {
  41.                 for(var i=0;i<this.arrCtl.length;i++)
  42.                 {
  43.                     this.arrCtl[i].style.textAlign='right';
  44.                     this.arrCtl[i].onkeypress = this.setKeyPress;
  45.                     this.arrCtl[i].onfocus = this.setFocus ;
  46.                     this.arrCtl[i].onblur= this.setBlur;
  47.                 }
  48.             }
  49.         };
  50.  
  51.         XFormatNumber._initialized = true;
  52.     }
  53. }
  54. XFormatNumber.prototype = new XFormat();
  55.  
  56. function XFormatMoney(arrayCtl)
  57. {
  58.     XFormatNumber.call(this,arrayCtl);
  59.     if (typeof XFormatMoney._initialized == 'undefined') 
  60.     {    
  61.         XFormatNumber.prototype.setKeyPress = function()
  62.         {
  63.             //mengizinkan karakter '-'(minus),'.'(separator desimal),'0-9'(numerik)
  64.             if(event.keyCode >=45 && event.keyCode<=46)
  65.                 return true;
  66.             else if(event.keyCode>=48 && event.keyCode<=57)
  67.                 return true;
  68.             else
  69.                 return false;
  70.         };
  71.  
  72.         XFormatMoney.prototype.setFocus =  this.getOriginalValue;
  73.  
  74.         XFormatMoney.prototype.setBlur = function()
  75.         {
  76.             var nStr = this.value+'';
  77.             var rgx = /(\d+)(\d{3})/;
  78.             var x;
  79.             if(this.value=='' || isNaN(this.value))
  80.                 {this.value = '0.00';}
  81.             else
  82.             {
  83.                 x = nStr.split('.');
  84.                 var x1 = x[0];
  85.                 var x2 = x.length > 1 ? '.' + x[1] : '.00';
  86.                 while (rgx.test(x1)) 
  87.                 {
  88.                     x1 = x1.replace(rgx, '$1' + ',' + '$2');
  89.                 }
  90.                 if(x.length > 1 && x[1].length >2)
  91.                     x2 = '.'+x[1].substring(0,2);
  92.                 this.value = x1 + x2;
  93.             }
  94.         };
  95.  
  96.         XFormatMoney._initialized = true;
  97.     }
  98. }
  99. XFormatMoney.prototype = new XFormatNumber();
  100. XFormatMoney.prototype.getOriginalValue = function()
  101. {
  102.     var originalValue=0;
  103.     var re = /,/;
  104.     if(this.value!='')
  105.     {
  106.         originalValue=this.value.replace(re,'');
  107.         if(parseFloat(originalValue).length != originalValue)
  108.             originalValue = parseFloat(originalValue);
  109.     }
  110.     this.value = originalValue;
  111.     this.select();
  112. }
  113.  
and this is how to use that functions

Expand|Select|Wrap|Line Numbers
  1. txtMoney1 = document.getElementById('txtMoney1');
  2. txtMoney2 = document.getElementById('txtMoney2');
  3. txtNumber1 = document.getElementById('txtNumber1');
  4. txtNumber2 = document.getElementById('txtNumber2');
  5.  
  6. oXFormatNumber = new XFormatNumber([txtNumber1,txtNumber2]);
  7. oXFormatNumber.setFormat();
  8. oXFormatMoney =new XFormatMoney([txtMoney1,txtMoney2]);
  9. oXFormatMoney.setFormat();
  10.  
  11.  
Aug 11 '08 #1
0 6318

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Trevor.Dhu | last post by:
I am just starting to use python, and as such my question may well be a bit simplistic. I am currently trying to write what should be a very basic set of routines for reading and writing to a...
2
by: Abhishek | last post by:
All, I am trying to export an access table to a text file. This table has a numeric field (Double) and when I export it to a text file, I see the number in the scientific format. MS-Access...
3
by: Ken Durden | last post by:
Is it possible to force positive values to have the + sign prefixed on them? double f1 = 1024.2; double f2 = -1024.2; string.Format( "{0:F}", f1 ); // +1024.2 string.Format( "{0:F}", f2 );...
7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
11
by: Keith | last post by:
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely reading them all - but I think I have a differing...
0
by: msnews.Microsoft.com | last post by:
Hi , Not able to figure out the way to format numeric fields. Code snippet Dim ColStyle3 As New DataGridTextBoxColumn With ColStyle3
3
by: Aaron Smith | last post by:
I found an quick and dirty example of a numeric text box that converts the string to a currency mask.. Here is the code: Public Class NumericMaskedTextBox Inherits System.Windows.Forms.TextBox ...
4
by: Sam | last post by:
Hi everyone I have a question regarding string format. If I want to write a set of numeric values from arrays to a text file with the "right alignment" format as below, can it be done?...
20
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
0
by: naorem ranjan | last post by:
I have one problem regarding format of mixed data columns in excel. I am using ADO.NET to retrieve data from excel sheet. Connection String used is: ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.