javascript string manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • YT

    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='JavaS cript'>
    <!-- // Hide from old browsers ...
    function Formsubmit () {
    document.frm_q2 .submit();
    return;
    }

    function Initialize() {
    frm_q2.q2_debt_ homeequity.valu e = "<%=Session("q2 _debt_homeequit y")%>";
    frm_q2.q2_debt_ banks.value = "<%=Session("q2 _debt_banks")%> ";
    frm_q2.q2_debt_ otherfinancial. value = "<%=Session("q2 _debt_otherfina ncial")%>";
    frm_q2.q2_debt_ creditcards.val ue = "<%=Session("q2 _debt_creditcar ds")%>";
    frm_q2.q2_debt_ government.valu e = "<%=Session("q2 _debt_governmen t")%>";
    frm_q2.q2_debt_ spouse.value = "<%=Session("q2 _debt_spouse")% >";
    frm_q2.q2_debt_ familyorfriends .value = "<%=Session("q2 _debt_familyorf riends")%>";
    frm_q2.q2_debt_ formerowners.va lue = "<%=Session("q2 _debt_formerown ers")%>";
    frm_q2.q2_debt_ other.value = "<%=Session("q2 _debt_other")%> ";

    frm_q2.q2_equit y_ownersavings. value = "<%=Session("q2 _equity_ownersa vings")%>";
    frm_q2.q2_equit y_spouse.value = "<%=Session("q2 _equity_spouse" )%>";
    frm_q2.q2_equit y_familyorfrien ds.value = "<%=Session("q2 _equity_familyo rfriends")%>";
    frm_q2.q2_equit y_formerowners. value = "<%=Session("q2 _equity_formero wners")%>";
    frm_q2.q2_equit y_investors.val ue = "<%=Session("q2 _equity_investo rs")%>";
    frm_q2.q2_equit y_other.value = "<%=Session("q2 _equity_other") %>";

    DebtCheck();
    EquityCheck();
    }

    function DebtCheck() {
    if ( isNaN( frm_q2.q2_debt_ homeequity.valu e ) ) frm_q2.q2_debt_ homeequity.valu e = "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.val ue ) ) frm_q2.q2_debt_ creditcards.val ue = "0";
    if ( isNaN( frm_q2.q2_debt_ government.valu e ) ) frm_q2.q2_debt_ government.valu e = "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.va lue ) ) frm_q2.q2_debt_ formerowners.va lue = "0";
    if ( isNaN( frm_q2.q2_debt_ other.value ) ) frm_q2.q2_debt_ other.value = "0";

    if ( frm_q2.q2_debt_ homeequity.valu e == "" ) frm_q2.q2_debt_ homeequity.valu e = "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.val ue == "" ) frm_q2.q2_debt_ creditcards.val ue = "0";
    if ( frm_q2.q2_debt_ government.valu e == "" ) frm_q2.q2_debt_ government.valu e = "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.va lue == "" ) frm_q2.q2_debt_ formerowners.va lue = "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.valu e ) + parseFloat( frm_q2.q2_debt_ banks.value ) + parseFloat( frm_q2.q2_debt_ otherfinancial. value ) + parseFloat( frm_q2.q2_debt_ creditcards.val ue ) + parseFloat( frm_q2.q2_debt_ government.valu e ) + parseFloat( frm_q2.q2_debt_ spouse.value ) + parseFloat( frm_q2.q2_debt_ familyorfriends .value ) + parseFloat( frm_q2.q2_debt_ formerowners.va lue ) + parseFloat( frm_q2.q2_debt_ other.value ) ;

    TotalCheck();
    }

    function EquityCheck() {
    if ( isNaN( frm_q2.q2_equit y_ownersavings. value ) ) frm_q2.q2_equit y_ownersavings. value = "0";
    if ( isNaN( frm_q2.q2_equit y_spouse.value ) ) frm_q2.q2_equit y_spouse.value = "0";
    if ( isNaN( frm_q2.q2_equit y_familyorfrien ds.value ) ) frm_q2.q2_equit y_familyorfrien ds.value = "0";
    if ( isNaN( frm_q2.q2_equit y_formerowners. value ) ) frm_q2.q2_equit y_formerowners. value = "0";
    if ( isNaN( frm_q2.q2_equit y_investors.val ue ) ) frm_q2.q2_equit y_investors.val ue = "0";
    if ( isNaN( frm_q2.q2_equit y_other.value ) ) frm_q2.q2_equit y_other.value = "0";

    if ( frm_q2.q2_equit y_ownersavings. value == "" ) frm_q2.q2_equit y_ownersavings. value = "0";
    if ( frm_q2.q2_equit y_spouse.value == "" ) frm_q2.q2_equit y_spouse.value = "0";
    if ( frm_q2.q2_equit y_familyorfrien ds.value == "" ) frm_q2.q2_equit y_familyorfrien ds.value = "0";
    if ( frm_q2.q2_equit y_formerowners. value == "" ) frm_q2.q2_equit y_formerowners. value = "0";
    if ( frm_q2.q2_equit y_investors.val ue == "" ) frm_q2.q2_equit y_investors.val ue = "0";
    if ( frm_q2.q2_equit y_other.value == "" ) frm_q2.q2_equit y_other.value = "0";


    //alert("Please check your email details are correct before submitting")
    frm_q2.q2_equit y_subtotal.valu e = parseFloat( frm_q2.q2_equit y_ownersavings. value ) + parseFloat( frm_q2.q2_equit y_spouse.value ) + parseFloat( frm_q2.q2_equit y_familyorfrien ds.value ) + parseFloat( frm_q2.q2_equit y_formerowners. value ) + parseFloat( frm_q2.q2_equit y_investors.val ue ) + parseFloat( frm_q2.q2_equit y_other.value );

    TotalCheck();
    }

    function TotalCheck() {
    frm_q2.q2_total .value = parseFloat( frm_q2.q2_debt_ subtotal.value ) + parseFloat( frm_q2.q2_equit y_subtotal.valu e );
    }
    //-->
    </SCRIPT>

  • N Clements

    #2
    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

    nospam@spam.spa m.spam.242.mail shell.com
    Remove 2nd through 4th spam to reply.


    "YT" <yt@MAPSONfunky chickens.org> wrote in
    news:xUbWa.933$ Jf3.10807@newsc ontent-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='JavaS cript'>
    > <!-- // Hide from old browsers ...
    > function Formsubmit () {
    > document.frm_q2 .submit();
    > return;
    > }
    >
    > function Initialize() {
    > frm_q2.q2_debt_ homeequity.valu e =
    > "<%=Session("q2 _debt_homeequit y")%>"; frm_q2.q2_debt_ banks.value =
    > "<%=Session("q2 _debt_banks")%> ";
    > frm_q2.q2_debt_ otherfinancial. value =
    > "<%=Session("q2 _debt_otherfina ncial")%>";
    > frm_q2.q2_debt_ creditcards.val ue =
    > "<%=Session("q2 _debt_creditcar ds")%>";
    > frm_q2.q2_debt_ government.valu e =
    > "<%=Session("q2 _debt_governmen t")%>"; frm_q2.q2_debt_ spouse.value
    > = "<%=Session("q2 _debt_spouse")% >";
    > frm_q2.q2_debt_ familyorfriends .value =
    > "<%=Session("q2 _debt_familyorf riends")%>";
    > frm_q2.q2_debt_ formerowners.va lue =
    > "<%=Session("q2 _debt_formerown ers")%>"; frm_q2.q2_debt_ other.value
    > = "<%=Session("q2 _debt_other")%> ";
    >
    > frm_q2.q2_equit y_ownersavings. value =
    > "<%=Session("q2 _equity_ownersa vings")%>";
    > frm_q2.q2_equit y_spouse.value =
    > "<%=Session("q2 _equity_spouse" )%>";
    > frm_q2.q2_equit y_familyorfrien ds.value =
    > "<%=Session("q2 _equity_familyo rfriends")%>";
    > frm_q2.q2_equit y_formerowners. value =
    > "<%=Session("q2 _equity_formero wners")%>";
    > frm_q2.q2_equit y_investors.val ue =
    > "<%=Session("q2 _equity_investo rs")%>";
    > frm_q2.q2_equit y_other.value = "<%=Session("q2 _equity_other") %>";
    >
    > DebtCheck();
    > EquityCheck();
    > }
    >
    > function DebtCheck() {
    > if ( isNaN( frm_q2.q2_debt_ homeequity.valu e ) )
    > frm_q2.q2_debt_ homeequity.valu e = "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.val ue ) )
    > frm_q2.q2_debt_ creditcards.val ue = "0"; if ( isNaN(
    > frm_q2.q2_debt_ government.valu e ) )
    > frm_q2.q2_debt_ government.valu e = "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.va lue ) )
    > frm_q2.q2_debt_ formerowners.va lue = "0"; if ( isNaN(
    > frm_q2.q2_debt_ other.value ) ) frm_q2.q2_debt_ other.value = "0";
    >
    > if ( frm_q2.q2_debt_ homeequity.valu e == "" )
    > frm_q2.q2_debt_ homeequity.valu e = "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.val ue == "" )
    > frm_q2.q2_debt_ creditcards.val ue = "0"; if (
    > frm_q2.q2_debt_ government.valu e == "" )
    > frm_q2.q2_debt_ government.valu e = "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.va lue == "" )
    > frm_q2.q2_debt_ formerowners.va lue = "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.valu e ) + parseFloat(
    > frm_q2.q2_debt_ banks.value ) + parseFloat(
    > frm_q2.q2_debt_ otherfinancial. value ) + parseFloat(
    > frm_q2.q2_debt_ creditcards.val ue ) + parseFloat(
    > frm_q2.q2_debt_ government.valu e ) + parseFloat(
    > frm_q2.q2_debt_ spouse.value ) + parseFloat(
    > frm_q2.q2_debt_ familyorfriends .value ) + parseFloat(
    > frm_q2.q2_debt_ formerowners.va lue ) + parseFloat(
    > frm_q2.q2_debt_ other.value ) ;
    >
    > TotalCheck();
    > }
    >
    > function EquityCheck() {
    > if ( isNaN( frm_q2.q2_equit y_ownersavings. value ) )
    > frm_q2.q2_equit y_ownersavings. value = "0"; if ( isNaN(
    > frm_q2.q2_equit y_spouse.value ) ) frm_q2.q2_equit y_spouse.value =
    > "0"; if ( isNaN( frm_q2.q2_equit y_familyorfrien ds.value ) )
    > frm_q2.q2_equit y_familyorfrien ds.value = "0"; if ( isNaN(
    > frm_q2.q2_equit y_formerowners. value ) )
    > frm_q2.q2_equit y_formerowners. value = "0"; if ( isNaN(
    > frm_q2.q2_equit y_investors.val ue ) )
    > frm_q2.q2_equit y_investors.val ue = "0"; if ( isNaN(
    > frm_q2.q2_equit y_other.value ) ) frm_q2.q2_equit y_other.value =
    > "0";
    >
    > if ( frm_q2.q2_equit y_ownersavings. value == "" )
    > frm_q2.q2_equit y_ownersavings. value = "0"; if (
    > frm_q2.q2_equit y_spouse.value == "" )
    > frm_q2.q2_equit y_spouse.value = "0"; if (
    > frm_q2.q2_equit y_familyorfrien ds.value == "" )
    > frm_q2.q2_equit y_familyorfrien ds.value = "0"; if (
    > frm_q2.q2_equit y_formerowners. value == "" )
    > frm_q2.q2_equit y_formerowners. value = "0"; if (
    > frm_q2.q2_equit y_investors.val ue == "" )
    > frm_q2.q2_equit y_investors.val ue = "0"; if (
    > frm_q2.q2_equit y_other.value == "" ) frm_q2.q2_equit y_other.value
    > = "0";
    >
    >
    > //alert("Please check your email details are correct before
    > submitting") frm_q2.q2_equit y_subtotal.valu e = parseFloat(
    > frm_q2.q2_equit y_ownersavings. value ) + parseFloat(
    > frm_q2.q2_equit y_spouse.value ) + parseFloat(
    > frm_q2.q2_equit y_familyorfrien ds.value ) + parseFloat(
    > frm_q2.q2_equit y_formerowners. value ) + parseFloat(
    > frm_q2.q2_equit y_investors.val ue ) + parseFloat(
    > frm_q2.q2_equit y_other.value );
    >
    > TotalCheck();
    > }
    >
    > function TotalCheck() {
    > frm_q2.q2_total .value = parseFloat( frm_q2.q2_debt_ subtotal.value
    > ) + parseFloat( frm_q2.q2_equit y_subtotal.valu e );
    > }
    > //-->
    > </SCRIPT>
    >
    >[/color]

    Comment

    Working...