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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript to change format on a field 2

Status
Not open for further replies.

svsuthar1

Programmer
Jul 6, 2004
135
US
Hi all,

Is there a way to change unformated field to currency after you leave the field... or it comes prefield in currency format.

Thanks,

Samir
 
>>or it comes prefield in currency format.
i dont understand what u want, but u may be looking for the onblur method...

Known is handfull, Unknown is worldfull
 
I am looking for a field on my form to change to currency after the person leaves the form... for example person will enter 1.9 so after tabing out of the field to the next it changes to $1.90.

Samir
 
i will give u the code for adding the dollar but the trailing 0 will take some time:
<input type="text" onblur="this.value='$'+this.value".....

Known is handfull, Unknown is worldfull
 
I think this will work fine... if you can add the trailing zero that would be nice but no hurry... This is lot better then not having it at all.

Thanks alot vbkris

Samir
 
o.k, here is the script i wrote, there may be a built in function in the math object for this, but i felt too lazy to search for one:

Code:
<script>
function ChangeVal(TheTextBox)
{
	TheVal=TheTextBox.value
	TheVal=TheVal.replace("$","")
	if(TheVal!="")
	{
		if(TheVal.indexOf(".")!=-1)
		{
			if(TheVal.match(/\.\d$/))
			{
				TheVal=TheVal+"0"
			}
		}
		else
		{
			TheVal=TheVal+".00"
		}
	}
	TheTextBox.value="$"+TheVal
}
</script>
<input type="text" onblur="ChangeVal(this)" name="TheTxt">


Note:
It follows only upto 2 decimal places...

Known is handfull, Unknown is worldfull
 
Here's my solution, not to undermine vbkris', just to add another alternative.

It makes sure a $ exists (will add one if one doesn't exist).
It makes sure only one decimal place is in there.
It makes sure no invalid characters are in there.
It will add 1 or 2 zeros, 1 if number is 1.3, two if number is 1.

Hope this helps:

Code:
function format(t) {
    var fs = '$';
	var l = '';
	var start = 0;

	var foundDecimal = false;
	var numAfterDecimal = 0;

	if (t.value.substr(0, 1) == '$') start = 1;

	for (var i = start; i < t.value.length; i++) {
        l = t.value.substr(i, 1);
		
        if (!isNaN(l)) {
            fs += l;
			if (foundDecimal)
			    numAfterDecimal++;
		} else {
            if (l == "." && !foundDecimal) {
			    fs += l;
				foundDecimal = true;
			} else {
                alert('Invalid number!');
				return false;
			}
		}
	}

	if (numAfterDecimal < 2) {
	    for (var i = 0; i < 2 - numAfterDecimal; i++)
			fs += '0';
	}
	
	t.value = fs;
}

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Call mine like this:

Code:
<input type="text" name="curr" onblur="format(this);"/>

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
where is the scope for undermining? we are all learning.

Known is handfull, Unknown is worldfull

Known is handfull, Unknown is worldfull
 
huh?

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Known is handfull, Unknown is worldfull

thats just my way of saying that i am a learner...

Known is handfull, Unknown is worldfull
 
ahh, gotcha :)

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Thanks alot a guys this is exactly i wanted... the only thing is that if you go through the field again with out entering it will automatically add $00 in it is there a way to stop that.


Thanks,

Samir
 
here's mine:
Code:
<script>
function ChangeVal(TheTextBox)
{
	TheVal=TheTextBox.value
	TheVal=TheVal.replace("$","")
	if(TheVal!="")
	{
		if(TheVal.indexOf(".")!=-1)
		{
			if(TheVal.match(/\.\d$/))
			{
				TheVal=TheVal+"0"
			}
		}
		else
		{
			TheVal=TheVal+".00"
		}
		TheTextBox.value="$"+TheVal
	}
}
</script>
<input type="text" onblur="ChangeVal(this)" name="TheTxt">

Known is handfull, Unknown is worldfull
 
And mine:

Code:
function format(t) {
    var fs = '$';
	var l = '';
	var start = 0;

	var foundDecimal = false;
	var numAfterDecimal = 0;

    if (t.value.length > 0) {
		if (t.value.substr(0, 1) == '$') start = 1;

		for (var i = start; i < t.value.length; i++) {
			l = t.value.substr(i, 1);
			
			if (!isNaN(l)) {
				fs += l;
				if (foundDecimal)
					numAfterDecimal++;
			} else {
				if (l == "." && !foundDecimal) {
					fs += l;
					foundDecimal = true;
				} else {
					alert('Invalid number!');
					return false;
				}
			}
		}

		if (numAfterDecimal < 2) {
			for (var i = 0; i < 2 - numAfterDecimal; i++)
				fs += '0';
		}
		
		t.value = fs;
	}
}

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Thanks both of the solution works so thanks a lot i think this place is very helpful to learn new stuff..

I really appreciate your help guys.

Samir
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top