validate field and fix NaN error

small shopping cart issue - users can enter the amount for the gift voucher.  i validate the input but if the user enters an incorrect format i get NaN returned.  How can I replace this with just 0.00?
function validatePrice() 
{
	function toPoundsAndPennys(n)
	{
		n = n.replace(/[^\d\.+-]/g,'');
        
		if(/\d*\.\d*\./.test(n)) 
			n = n.replace(/(\d*\.\d*)\./,'$1');
         
		n = n.replace(/(\.\d{2})[\d.]*/g,'$1');
        
		return n;
	}
	
	var price = toPoundsAndPennys(document.form.gift-vouchers-price.value);
	
	if(isNaN(price))
	{
		price = 0.00;
	}
	
	document.form.gift-vouchers-price.value = price;
	
	return;
}

Open in new window

LVL 16
ellandrdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PhatzerCommented:
Have you tried this instead? You code seems OK, so there must be something silly wrong!
        if(isNaN(price) == true)
        {
                price = 0.00;
        }

Open in new window

0
pkumarraCommented:
Thsi statement will generate an error because of gift-vouchers-price, which is an illegal identifier
var price = toPoundsAndPennys(document.form.gift-vouchers-price.value);

Change this "gift-vouchers-price" to a valid javascript variable name.
Follow the following rules for javascript variable names

The first character must be a letter or an underscore (_). You can't use a number as the first character.

The rest of the variable name can include any letter, any number, or the underscore. You can't use any other characters, including spaces, symbols, and punctuation marks.
0
ellandrdAuthor Commented:
ok i correct my JS variable names and i still have the same issue.

heres my input field:

<input type="text" value="0.00" class="shop-fields" name="gift_vouchers_price" onkeyup="javascript:validatePrice();" onchange="this.value=(1 * this.value).toFixed(2);" onblur="this.value=(1 * this.value).toFixed(2);"

the rest of the code from above is unchanged...
0
shiraztiCommented:
Try this.......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
 
<script language="javascript">
 
function validatePrice() 
{
 
        function toPoundsAndPennys(n)
        {
                n = n.replace(/[^\d\.+-]/g,'');
        
                if(/\d*\.\d*\./.test(n)) 
                        n = n.replace(/(\d*\.\d*)\./,'$1');
         
                n = n.replace(/(\.\d{2})[\d.]*/g,'$1');
        
                return n;
        }
        
        var price = toPoundsAndPennys(document.form1.gift_vouchers_price.value);
        
        if(isNaN(price) || price == null)
        {
                price = 0.00;
        }
        
        document.form1.gift_vouchers_price.value = price;
        
        return;
}
 
</script>
 
</head>
 
<body>
<form name="form1" id="form1">
 
<input type="text" value="0.00" class="shop-fields" name="gift_vouchers_price" onkeyup="javascript:validatePrice();" onchange="this.value=(1 * this.value).toFixed(2);" onblur="this.value=(1 * this.value).toFixed(2);"  />
 
</form>
 
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.