Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 10:27 AM
YT
Guest
 
Posts: n/a
Default javascript string manipulation

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>

  #2  
Old July 20th, 2005, 10:28 AM
N Clements
Guest
 
Posts: n/a
Default Re: javascript string manipulation

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
nospam@spam.spam.spam.242.mailshell.com
Remove 2nd through 4th spam to reply.


"YT" <yt@MAPSONfunkychickens.org> wrote in
news:xUbWa.933$Jf3.10807@newscontent-01.sprint.ca:
[color=blue]
> 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>
>
>[/color]

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles