4 Replies - 660 Views - Last Post: 17 August 2012 - 10:35 AM

#1 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 433
  • View blog
  • Posts: 3,010
  • Joined: 12-January 10

Javascript cannot begin with the ' ' character

Posted 17 August 2012 - 09:04 AM

function CommaFormatted(amount)
alert(amount);
{
var delimiter = ","; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';



[color="#FF0000"][b]if(i < 0) { minus = '-'; }[/b][/color]



i = Math.abs(i);
	var n = new String(i);
var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}

if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);


	
[color="#FF0000"][b]if(d.length < 1) { amount = n; }[/b][/color]



	amount = minus + amount;
	return amount;
	


}



error:

Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 1184, position 7. 



i narrowed it down to the highlighted areas above but can't seem to repair it. Any ideas would be welcomed.

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript cannot begin with the ' ' character

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Javascript cannot begin with the ' ' character

Posted 17 August 2012 - 09:10 AM

Take a closer look at line #2. It's after the function definition, but before the function body...
Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,110
  • Joined: 07-September 06

Re: Javascript cannot begin with the ' ' character

Posted 17 August 2012 - 09:17 AM

I believe that the code works fine for me, you just need to move the alert inside of the function (instead of between the function name and body (line 2)).

This code does assume that the number is in a specific format (ie. it has to have a decimal point, otherwise it will error out).

This works for me:
<script>
function CommaFormatted(amount)
{
var delimiter = ","; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';



if(i < 0) { minus = '-'; }



i = Math.abs(i);
	var n = new String(i);
var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}

if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);


	
if(d.length < 1) { amount = n; }



	amount = minus + amount;
	return amount;
	


}
alert(CommaFormatted("123456."));
</script>


Hopefully that helps.
Was This Post Helpful? 0
  • +
  • -

#4 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 433
  • View blog
  • Posts: 3,010
  • Joined: 12-January 10

Re: Javascript cannot begin with the ' ' character

Posted 17 August 2012 - 09:39 AM

I got rid of the alert but I still get the same error.

it is tied in with this

function formatAsMoney(x)
{

	var money_value;
	mnt = x.value;
alert(mnt);
	mnt = mnt.replace(/\,/g, '');	
	mnt -= 0;
	mnt = (Math.round(mnt*100))/100;
	money_value = (mnt == Math.floor(mnt)) ? mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);

	if (isNaN(money_value)) 
	{
		x.value = "0";
	}else{
		money_value = CommaFormatted(money_value);		
		x.value = money_value.replace(".00", "");
	}
}


when i add the comma function it gives that error.
the alerts in the above retunr the correct info until the comma function is put in then it errors out

This post has been edited by DarenR: 17 August 2012 - 09:43 AM

Was This Post Helpful? 0
  • +
  • -

#5 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 433
  • View blog
  • Posts: 3,010
  • Joined: 12-January 10

Re: Javascript cannot begin with the ' ' character

Posted 17 August 2012 - 10:35 AM

the repair for this is taking all the < and > and making them &lt; &gt;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1