javascript string manipulation

Y

YT

Hello,

Here's the situation:
I have several fields split into 2 sections (Debt & Equity) on a form that should only take numerical values. The page is a mix of Javascript & ASP such that when the user visits the page, the initial values of the field are filled in by Session cookies. When the cursor leaves a field, the code checks to see if the entry is a number, and if it is not then it replaces it with a "0". Then it checks each field for empty entries and when it finds one, it replaces it with an "0". Then it totals up the entries in a sub-total and then totals up the two sub-totals.

See the code below for details (should be self-explanatory).

The goal is to modify the code to find out if the user has entered in a comma for one of the numbers. Ie. these are all dollar values, and as such when someone enters in 10,000 the code resets that field to a zero. what I'd like to do is to modify the code such that the ** check process looks for commas in each field AND removes them ** before number checking and blank entry checking.


Can you help me?! Thanks in advance!!!!!

yt



CURRENT CODE:

<SCRIPT LANGUAGE='JavaScript'>
<!-- // Hide from old browsers ...
function Formsubmit () {
document.frm_q2.submit();
return;
}

function Initialize() {
frm_q2.q2_debt_homeequity.value = "<%=Session("q2_debt_homeequity")%>";
frm_q2.q2_debt_banks.value = "<%=Session("q2_debt_banks")%>";
frm_q2.q2_debt_otherfinancial.value = "<%=Session("q2_debt_otherfinancial")%>";
frm_q2.q2_debt_creditcards.value = "<%=Session("q2_debt_creditcards")%>";
frm_q2.q2_debt_government.value = "<%=Session("q2_debt_government")%>";
frm_q2.q2_debt_spouse.value = "<%=Session("q2_debt_spouse")%>";
frm_q2.q2_debt_familyorfriends.value = "<%=Session("q2_debt_familyorfriends")%>";
frm_q2.q2_debt_formerowners.value = "<%=Session("q2_debt_formerowners")%>";
frm_q2.q2_debt_other.value = "<%=Session("q2_debt_other")%>";

frm_q2.q2_equity_ownersavings.value = "<%=Session("q2_equity_ownersavings")%>";
frm_q2.q2_equity_spouse.value = "<%=Session("q2_equity_spouse")%>";
frm_q2.q2_equity_familyorfriends.value = "<%=Session("q2_equity_familyorfriends")%>";
frm_q2.q2_equity_formerowners.value = "<%=Session("q2_equity_formerowners")%>";
frm_q2.q2_equity_investors.value = "<%=Session("q2_equity_investors")%>";
frm_q2.q2_equity_other.value = "<%=Session("q2_equity_other")%>";

DebtCheck();
EquityCheck();
}

function DebtCheck() {
if ( isNaN( frm_q2.q2_debt_homeequity.value ) ) frm_q2.q2_debt_homeequity.value = "0";
if ( isNaN( frm_q2.q2_debt_banks.value ) ) frm_q2.q2_debt_banks.value = "0";
if ( isNaN( frm_q2.q2_debt_otherfinancial.value ) ) frm_q2.q2_debt_otherfinancial.value = "0";
if ( isNaN( frm_q2.q2_debt_creditcards.value ) ) frm_q2.q2_debt_creditcards.value = "0";
if ( isNaN( frm_q2.q2_debt_government.value ) ) frm_q2.q2_debt_government.value = "0";
if ( isNaN( frm_q2.q2_debt_spouse.value ) ) frm_q2.q2_debt_spouse.value = "0";
if ( isNaN( frm_q2.q2_debt_familyorfriends.value ) ) frm_q2.q2_debt_familyorfriends.value = "0";
if ( isNaN( frm_q2.q2_debt_formerowners.value ) ) frm_q2.q2_debt_formerowners.value = "0";
if ( isNaN( frm_q2.q2_debt_other.value ) ) frm_q2.q2_debt_other.value = "0";

if ( frm_q2.q2_debt_homeequity.value == "" ) frm_q2.q2_debt_homeequity.value = "0";
if ( frm_q2.q2_debt_banks.value == "" ) frm_q2.q2_debt_banks.value = "0";
if ( frm_q2.q2_debt_otherfinancial.value == "" ) frm_q2.q2_debt_otherfinancial.value = "0";
if ( frm_q2.q2_debt_creditcards.value == "" ) frm_q2.q2_debt_creditcards.value = "0";
if ( frm_q2.q2_debt_government.value == "" ) frm_q2.q2_debt_government.value = "0";
if ( frm_q2.q2_debt_spouse.value == "" ) frm_q2.q2_debt_spouse.value = "0";
if ( frm_q2.q2_debt_familyorfriends.value == "" ) frm_q2.q2_debt_familyorfriends.value = "0";
if ( frm_q2.q2_debt_formerowners.value == "" ) frm_q2.q2_debt_formerowners.value = "0";
if ( frm_q2.q2_debt_other.value == "" ) frm_q2.q2_debt_other.value = "0";


//alert("Please check your email details are correct before submitting")
frm_q2.q2_debt_subtotal.value = parseFloat( frm_q2.q2_debt_homeequity.value ) + parseFloat( frm_q2.q2_debt_banks.value ) + parseFloat( frm_q2.q2_debt_otherfinancial.value ) + parseFloat( frm_q2.q2_debt_creditcards.value ) + parseFloat( frm_q2.q2_debt_government.value ) + parseFloat( frm_q2.q2_debt_spouse.value ) + parseFloat( frm_q2.q2_debt_familyorfriends.value ) + parseFloat( frm_q2.q2_debt_formerowners.value ) + parseFloat( frm_q2.q2_debt_other.value ) ;

TotalCheck();
}

function EquityCheck() {
if ( isNaN( frm_q2.q2_equity_ownersavings.value ) ) frm_q2.q2_equity_ownersavings.value = "0";
if ( isNaN( frm_q2.q2_equity_spouse.value ) ) frm_q2.q2_equity_spouse.value = "0";
if ( isNaN( frm_q2.q2_equity_familyorfriends.value ) ) frm_q2.q2_equity_familyorfriends.value = "0";
if ( isNaN( frm_q2.q2_equity_formerowners.value ) ) frm_q2.q2_equity_formerowners.value = "0";
if ( isNaN( frm_q2.q2_equity_investors.value ) ) frm_q2.q2_equity_investors.value = "0";
if ( isNaN( frm_q2.q2_equity_other.value ) ) frm_q2.q2_equity_other.value = "0";

if ( frm_q2.q2_equity_ownersavings.value == "" ) frm_q2.q2_equity_ownersavings.value = "0";
if ( frm_q2.q2_equity_spouse.value == "" ) frm_q2.q2_equity_spouse.value = "0";
if ( frm_q2.q2_equity_familyorfriends.value == "" ) frm_q2.q2_equity_familyorfriends.value = "0";
if ( frm_q2.q2_equity_formerowners.value == "" ) frm_q2.q2_equity_formerowners.value = "0";
if ( frm_q2.q2_equity_investors.value == "" ) frm_q2.q2_equity_investors.value = "0";
if ( frm_q2.q2_equity_other.value == "" ) frm_q2.q2_equity_other.value = "0";


//alert("Please check your email details are correct before submitting")
frm_q2.q2_equity_subtotal.value = parseFloat( frm_q2.q2_equity_ownersavings.value ) + parseFloat( frm_q2.q2_equity_spouse.value ) + parseFloat( frm_q2.q2_equity_familyorfriends.value ) + parseFloat( frm_q2.q2_equity_formerowners.value ) + parseFloat( frm_q2.q2_equity_investors.value ) + parseFloat( frm_q2.q2_equity_other.value );

TotalCheck();
}

function TotalCheck() {
frm_q2.q2_total.value = parseFloat( frm_q2.q2_debt_subtotal.value ) + parseFloat( frm_q2.q2_equity_subtotal.value );
}
//-->
</SCRIPT>
 
N

N Clements

Write and implement a function similar to the following:

function removeComma( val )
{
re = /,/gi;
return( val.replace( re, "" ));
}

Hope that helps.

N. Clements
Brainbench MVP for _Javascript
www.brainbench.com
(e-mail address removed).242.mailshell.com
Remove 2nd through 4th spam to reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,631
Members
45,340
Latest member
Thalia56P0

Latest Threads

Top