We help IT Professionals succeed at work.

validating currency - without commas

-Dman100-
-Dman100- asked
on
I am validating an input that should be a currency.  The input should not contain the currency sign "$" or any commas.  Just straight decimal.  So, if the user was going to enter $1,000.00, it should just be 1000.00 or 1000.

If am using parseFloat and checking if isNaN.  This works pretty well except that if a user enters 1,000.00 then it would actually take the first character 1 as a valid and scratch everything else after the "1".  I need to catch the comma and make sure that the entire number is trapped as invalid.

Here is what I was trying (see below)

Thanks for any help.
 
<apex:inputText value="{!Account.Additional_Donation_Amount__c}" id="additionalContribution" onblur="return isNumber();" onchange="adj_amount();" />

    function isNumber() {
    	var flag = true;
    	var numStr = document.getElementById('page1:Details:additionalContribution');
    	var error_additionalContribution = document.getElementById('err_additionalContribution');
		if(isNaN(parseFloat(numStr.value)) && numStr.value != '') {
			error_additionalContribution.style.display='block';
            flag =false;
		}
		else {
			error_additionalContribution.style.display='none';
		}	
		
		if(flag==false){
            return flag;
        }else{
            return flag;
        }
    }


function adj_amount()
{
    var eventType = "{!sfdcEvent.Type__c}";
    var panelhost = document.getElementById('page1:Details:outputpanelHost');
    var panelpatron = document.getElementById('page1:Details:outputpanelPatron');
    var panelguest = document.getElementById('page1:Details:outputpanelGuest');

    var cat = document.getElementById('page1:Details:category');
    var amount = document.getElementById('page1:Details:fee1');
    var new_amount = document.getElementById('page1:Details:fee1');
    var adddonnasel = document.getElementById('page1:Details:adddonnasel');
    
    amount.innerHTML = 0;
    
    if (cat.value == 'host')
    {
   		var host = document.getElementById('page1:Details:host');
		if (host.value == "emerald") { 
			amount = parseInt(amount.innerHTML) + 50000;
			amount = amount.toFixed(2); 
			new_amount.innerHTML = String("$" + amount);
		}
    	else if(host.value == "diamond") { 
    		amount = parseInt(amount.innerHTML) + 25000;
    		amount = amount.toFixed(2); 
			new_amount.innerHTML = String("$" + amount); 
    	}
    	else if(host.value == "platinumplus") {	
    		amount = parseInt(amount.innerHTML) + 15000;
    		amount = amount.toFixed(2); 
			new_amount.innerHTML = String("$" + amount); 
    	}
    	else if(host.value == "platinum") {	
    		amount = parseInt(amount.innerHTML) + 10000;
    		amount = amount.toFixed(2); 
			new_amount.innerHTML = String("$" + amount); 
    	}
    	else if(host.value == "honorcircle") { 
    		amount = parseInt(amount.innerHTML) + 7500;
    		amount = amount.toFixed(2); 
			new_amount.innerHTML = String("$" + amount); 
    	}
    }
    else if(cat.value == 'patron')
    {
   		var patron = document.getElementById('page1:Details:patronpairsel');
		if (patron.value == "angel") { 
			amount = parseInt(amount.innerHTML) + 1500;
			amount = amount.toFixed(2); 
			new_amount.innerHTML = String("$" + amount); 
		}
    	else if(patron.value == "patroncouple") { 
    		amount = parseInt(amount.innerHTML) + 1000;
    		amount = amount.toFixed(2); 
			new_amount.innerHTML = String("$" + amount); 
    	}
    	else if(patron.value == "patron") {	
    		amount = parseInt(amount.innerHTML) + 500;
    		amount = amount.toFixed(2); 
			new_amount.innerHTML = String("$" + amount); 
    	}
    }
    else if(cat.value == "guest")
    {
		var patron = document.getElementById('page1:Details:patronpairsel');
		var contributionAmt = document.getElementById('page1:Details:additionalContribution');
		if (patron.value == "angel") { 
			if(!isNaN(parseFloat(contributionAmt.value)) && contributionAmt.value != null) {
				amount = parseInt(amount.innerHTML) + 1500 + parseInt(contributionAmt.value);
				amount = amount.toFixed(2); 
				new_amount.innerHTML = String("$" + amount);
			}
			else {
				amount = parseInt(amount.innerHTML) + 1500;
				amount = amount.toFixed(2); 
				new_amount.innerHTML = String("$" + amount); 
			}
		}
    	else if(patron.value == "patroncouple") {
    		if(!isNaN(parseFloat(contributionAmt.value)) && contributionAmt.value != null) { 
    			amount = parseInt(amount.innerHTML) + 1000 + parseInt(contributionAmt.value);
    			amount = amount.toFixed(2); 
				new_amount.innerHTML = String("$" + amount);
			}
			else {
				amount = parseInt(amount.innerHTML) + 1000;
    			amount = amount.toFixed(2); 
				new_amount.innerHTML = String("$" + amount);
			} 
    	}
    	else if(patron.value == "patron") {	
    		if(!isNaN(parseFloat(contributionAmt.value)) && contributionAmt.value != null) {
    			amount = parseInt(amount.innerHTML) + 500 + parseInt(contributionAmt.value);
    			amount = amount.toFixed(2); 
				new_amount.innerHTML = String("$" + amount);
			}
			else {
				amount = parseInt(amount.innerHTML) + 500;
    			amount = amount.toFixed(2); 
				new_amount.innerHTML = String("$" + amount);
			} 
    	}
    }
    else if (cat.value == '')
    {
    	amount = parseInt(amount.innerHTML) + 0;
		amount = amount.toFixed(2); 
		new_amount.innerHTML = String("$" + amount);
    }
    
    amount = amount.toFixed(2); 
	new_amount.innerHTML = String("$" + amount); 
	alert(amount);
	alert(new_amount);
}

Open in new window

Comment
Watch Question

Commented:
You can test if the input is what you need by doing like this.

alert(validate('1000'));
alert(validate('1000.00'));
alert(validate('1000.3'));
alert(validate('1,000'));
alert(validate('1,000.00'));
alert(validate('Hello'));

function validate(money) {
    return (/^[0-9]+(?:\.[0-9]{2})?$/.test(money));
}

Open in new window


Only the first two validates return true, the others return false.