Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formating text box on the fly

Status
Not open for further replies.

szuccaro

Programmer
Dec 17, 2002
84
US
I am trying to format text boxes in a form so
they dont have a float value..ie. 1.00236555..
Only need them to disply the number to its second
decimal place (money format).

I think this would be simple but am having a difficult
time finding number formating functions for javascript.
Any help or ides appreciated.- Steve
 
Steve,

There are no number formating functions in Javascript. You have to write them yourself. Have you tried? If your having trouble with your code post some of it and we can help you out.

Otherwise here is a Javascript class i use
Code:
function NumberFormat(){
	
	this.decimals = 2;
	this.preMask = "";
	this.postMask = "";
	this.commas = false;
	
	this.toString = nf_tostring;
	this._digitize = nf_digitize;
	this._insertCommas = nf_insCommas;
	this._parseDigits = nf_parseDigits;
	
	function nf_parseDigits( instr){
		var sret = new String("");
		for(nChr=0; nChr< instr.length; nChr++)
			if ( !isNaN(instr.charAt(nChr)) )
				sret += instr.charAt(nChr);
		return sret;
	}
	
	function nf_tostring( instr){
		if( 'string' != typeof(instr)) instr = new String(instr);
		var nums = instr.split(&quot;.&quot;);	//separate the number from the fraction
		
		if ( nums.length > 2)
			nums = new Array(nums[0], nums[1]);
			
		// make sure we have only digits on both sides of the decimal
		for(N=0; N< nums.length; N++)
			nums[N] = this._parseDigits(nums[N]);
			
		if ( this.commas)
			nums[0] = this._insertCommas(nums[0]);
		
		if ( 0 < this.decimals){
			if ( 2 > nums.length)
				nums[1] = new String(&quot;&quot;);
				
			nums[1] = this._digitize( nums[1]);
		}
		else
			nums = new Array(nums[0]);

		var sRet = new String(this.preMask);
	
		if ( nums.length > 1)
			sRet += nums.join(&quot;.&quot;);
						
		sRet += this.postMask;
			
		return sRet;
	}
	
	function nf_digitize( digits){
	
		var sret = new String(&quot;&quot;);
		for(nChr=0; nChr< digits.length && sret.length < this.decimals; nChr++)
			if ( !isNaN(digits.charAt(nChr)) )
				sret += digits.charAt(nChr);
				
		while( sret.length < this.decimals)
			sret += &quot;0&quot;;
			
		return sret;
	}
	
	function nf_insCommas( lnum){
	
		var rnum = new String(lnum);
		var sVal = new String(&quot;&quot;);
		// reverse the left number keeping only numeric values
		for(nChr=rnum.length; nChr>-1; nChr--){
			if ( !isNaN(rnum.charAt(nChr)) )
				sVal += rnum.charAt(nChr);
		}
		// if there were no digits assign a zero
		if ( 0 == sVal.length) sVal += &quot;0&quot;;
	
		// now reverse it again adding in commas
		rnum = new String(&quot;&quot;);
		for(nChr=0; nChr< sVal.length; nChr++){
			rnum = sVal.charAt(nChr) + rnum;
			switch( nChr){
			case 2:
			case 5:
			case 8:
			case 11:
				if ( nChr < sVal.length -1)
					rnum = &quot;,&quot; + rnum;
				break;
			default:
				break;
			}	// switch( for each comma)
		}	// for()
		
		return rnum;
	}	// fn_insCommas()
}	// NumberFormat class

-pete
 
I am currently using number functions. the big problem
I am having is that the text boxs are created on the fly
and im having trouble assining values to them. If there
is a way to put together a variable name or object on
the fly this would solve my problem.
EX:
n=1;

while(n<100)
{
&quot;document.form.text&quot; + n + &quot;.value&quot; = n + 15;
n++
}

..I know exactly what there names are going to be
cause when there made, a PHP script just addes the
current value of a counter vairable &quot;n&quot; to there
names. As I said before, I can get there value by
looping through thier names but cant seem so assign
a value to them (the formated number)...
 
PS: I was hoping there would be some number functions
I could use within the tag for the text box so I could
just change there value using OnBlur() event. But apparetly
I have to use a function at the top of my page to change the value, I could use function in each tag for each text box, buta thats alot of code to have in a simple tag for an input box :)
 
Maybe this example will help:
Code:
<html>
<SCRIPT LANGUAGE=javascript>
<!--
function format( e){
	var nf = new NumberFormat();
	// to change the format from the default, set the
	// nf properties before calling toString()
	e.value = nf.toString( e.value);
}
//-->
</SCRIPT>
<body>
<INPUT type=&quot;text&quot; onblur=&quot;format(this);&quot;>
</body></html>

-pete
 
Calling function using onChange:
=================================================
(all the &quot;\&quot; are for PHP so it dosnt get confused.

<input type=\&quot;text\&quot; name=\&quot;total$n\&quot; size=\&quot;12\&quot; onChange=\&quot;format_this(this);\&quot;>

Then heres the function:
=================================================
function format_this(valueOFtotal)
{
var a= new ToFmt(valueOFtotal);
valueOFtotal.value = a.fmtF(13,2);
}


Its not working though ????
 
I have got it to work!
Thanks for all your input.
-Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top