Hi there,
I am having a Javascript problem and any help would be apprecated.

I am trying to test the input for if length = 1 then add on a 0 as this would mean if the user wanted £1.20 if would come out as £1.20 and not £1.2 as it is at the moment.

I've tried concatenating the variables together and then putting them back into the variable but it still comes out at 1 decimal place.

I am testing this on the form validation page, not the form processing page at current.

if(cpence.value.length == 1){
	  cpence + 0;}

Also trying testing it before it adds it to the database, but still no luck.

if($pence.value.length == 1){
	$pence . "0";}

Many thanks

Number.toFixed(n) rounds a number to n decimal places.

Try Number($pence.value).toFixed(2) .

Airshow

Thanks very much for your reply, but there wasnt any luck with that either.

Ben,

Post the whole block of code - enough for me to see the context of the statement. I'll see if I can fix it.

Airshow

i use this if this helps

<script type="text/javascript">
function dm(amount)
{
string = "" + amount;
dec = string.length - string.indexOf('.');
if (string.indexOf('.') == -1)
return string + '.00';
if (dec == 1)
return string + '00';
if (dec == 2)
return string + '0';
if (dec > 3)
return string.substring(0,string.length-dec+3);
return string;
}
</script>

EDIT: please ignore - I misread Kardklub above as a reply to my last post.

Airshow

Hi there, thanks for the replies, heres the code im trying to get working....

function checkAdd(){
	
	var cname = document.getElementById('addname');
	var cpound = document.getElementById('addpound');
	var cpence = document.getElementById('addpence');
	var cpencee;
	var div = document.getElementById('errormsg');
	var lets = /^[a-zA-Z\s]+$/;
	var nums = /^[0-9\.]+$/;
	
	if(cname.value == ''){
		div.innerHTML= "<b>Please fill in the item name</b>";
		cname.focus();
		return false;
	}
	else if(cname.value.match(lets)){
		if(cpound.value == ''){
			cpound == 0}
				else if(cpound.value.match(nums)){
					if(cpence.value == ''){
						div.innerHTML= "<b>Please enter a value for p</b>";
						cpence.focus();
						return false;}
							else if((cpound.value.length == 1) && (cpence.value.length == 1)){
					>>>>>>>>>>>>>>>>>>> cpence + 0}
								else if(cpence.value.match(nums)){
									return true;}
								else{
									div.innerHTML= "<b>Only numbers please</b>";
									cpence.focus();
									return false;}}
							
				else{
					div.innerHTML= "<b>Only numbers please</b>";
					cpound.focus();
					return false;}}
	else{
	div.innerHTML= "<b>Only letters please</b>";
	cname.focus();
	return false;}
		return true;
}

Just to summarise, the user types int o2 text boxes, for pounds and pence. however it only goes to 1 decimal place, not 2, as a result this happens when printed fro mthe db.

Test £3.3
Test £3.2

I am trying to get it so that it notices that there was only 1 number input value.length == 1 and then add on a 0 to make it

Test £3.30
Test £3.20


Thanks

Aha, that's a bit different.

I think you need one of the folowing:

cpence.value += '0';//to multiply by 10
    cpence.value = '0' + cpence.value;//to pad left with a zero

The amended string will appear in the "addpence" input field.

Airshow

Thanks for the answer still no luck tho, Igot round it by saying:

else if((cpound.value.length == 1) && (cpence.value.length == 1)){
								div.innerHTML= "<b>Please enter 2 digits for pence</b>";
										cpence.focus();
										return false;}

But it still returns £2.0 therefore I thin kthe problem is with the database, although it is set to INT(2) it still only stores 1 zero.


EDIT: Fixed! when i finally thought about it, of course it was trashing the second zero as 00 isnt an interger.
set it to VARCHAR to store the 00 and it works perfectly, sorry for my incompetence on noticing this, I however wasnt the one who setup the database initially ;)

Thanks for your help AirShow, much appreciated

:cool:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.