Javascript Validation Function aint workin ;o(

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

    Javascript Validation Function aint workin ;o(

    Hi guys,

    Witten a function to check two input boxes, ensure that only one has a
    numerical value in, but I'm gettng the javascript error 'Object expected'??

    function
    Ash_CheckCommis sion(TheFormToC heck,TheFieldTo Check,TheFieldT oDisable) {
    if (eval('document .'+TheFormToChe ck+'.'+TheField ToCheck+'.value > 0 &&
    document.'+TheF ormToCheck+'.'+ TheFieldToDisab le+'.value > 0')) {
    alert('You may only enter a Commission Percentage OR Commission Fee, not
    both.');
    eval('document. 'TheFormToCheck +'.'+TheFieldTo Disable+'.value ="0"');
    }
    }

    I'm calling the function by putting it on the OnChange attribute of each
    text box, it DID work UNTIL I put the +TheFormToCheck + bits in there, prior
    to this I had hardcoded the name of the form into the function.

    Appreciate your help!

    Cheers, Ash



  • J. Hall

    #2
    Re: Javascript Validation Function aint workin ;o(

    Example of how I'm using this within the HTML itself...

    <input name="LinkCommi ssionPercentage " type="text"
    class="GenericF ormTextBox" id="LinkCommiss ionPercentage" value="<% If
    TheLinkCommissi onPercentage <> "" Then %><%= TheLinkCommissi onPercentage
    %><% Else %>0<% End If %>" size="5" maxlength="4"
    OnChange="Ash_C heckCommission( 'formaddproduct ','LinkCommissi onPercentage',' L
    inkCommissionFe e');">

    Cheers, Ash!



    "J. Hall" <remove_this_as h@a-hall.com> wrote in message
    news:spSdna-utrl-a27dRVn-ig@eclipse.net. uk...[color=blue]
    > Hi guys,
    >
    > Witten a function to check two input boxes, ensure that only one has a
    > numerical value in, but I'm gettng the javascript error 'Object[/color]
    expected'??[color=blue]
    >
    > function
    > Ash_CheckCommis sion(TheFormToC heck,TheFieldTo Check,TheFieldT oDisable) {
    > if (eval('document .'+TheFormToChe ck+'.'+TheField ToCheck+'.value > 0 &&
    > document.'+TheF ormToCheck+'.'+ TheFieldToDisab le+'.value > 0')) {
    > alert('You may only enter a Commission Percentage OR Commission Fee, not
    > both.');
    > eval('document. 'TheFormToCheck +'.'+TheFieldTo Disable+'.value ="0"');
    > }
    > }
    >
    > I'm calling the function by putting it on the OnChange attribute of each
    > text box, it DID work UNTIL I put the +TheFormToCheck + bits in there,[/color]
    prior[color=blue]
    > to this I had hardcoded the name of the form into the function.
    >
    > Appreciate your help!
    >
    > Cheers, Ash
    >
    >
    >[/color]


    Comment

    • kaeli

      #3
      Re: Javascript Validation Function aint workin ;o(

      In article <foGdnVdG36MJZ2 7dRVn-sQ@eclipse.net. uk>, remove_this_ash @a-
      hall.com enlightened us with...[color=blue]
      > Example of how I'm using this within the HTML itself...
      >
      > <input name="LinkCommi ssionPercentage " type="text"
      > class="GenericF ormTextBox" id="LinkCommiss ionPercentage" value="<% If
      > TheLinkCommissi onPercentage <> "" Then %><%= TheLinkCommissi onPercentage
      > %><% Else %>0<% End If %>" size="5" maxlength="4"
      > OnChange="Ash_C heckCommission( 'formaddproduct ','LinkCommissi onPercentage',' L
      > inkCommissionFe e');">
      >
      > Cheers, Ash!
      >[/color]

      Ewww, get all those evals out of there. Icky. ;)

      OnChange="Ash_C heckCommission( this, this.form.eleme nts
      ['LinkCommission Fee'])"

      function Ash_CheckCommis sion (TheFieldToChec k,TheFieldToDis able)
      {
      if (TheFieldToChec k.value > 0 &&
      TheFieldToDisab le.value > 0)
      {
      alert('You may only enter a Commission Percentage OR Commission
      fee, not both.');
      TheFieldToDisab le.value="0";
      }
      }

      Okay, now here's the thing - you're comparing a value to a number and
      setting a value to a string. Either it's a number or it's a string. Pick
      one.
      If it's a number, change the value comparisons above to use parseInt (or
      parseFloat) and set the value to 0, not "0". If it is a string, change
      the comparitors to strings. (0 to "0")
      Considering the field names, I'll assume they are supposed to be floats.

      if (parseFloat(The FieldToCheck.va lue,10) > 0 &&
      parseFloat(TheF ieldToDisable.v alue,10) > 0)

      TheFieldToDisab le.value=0;

      Warning: if the user might enter something that is not a number, check
      before using parseFloat. I recommend using regular expressions.

      HTH

      --
      --
      ~kaeli~
      The best part of having kids is giving them back to their
      parents.



      Comment

      • J. Hall

        #4
        Re: Javascript Validation Function aint workin ;o(

        Thanks for your quick and detailed response! I'm a little confused being a
        VB person, not Javascript and haven't yet come across the term 'Float', new
        territory!

        I don't quite understand though from your rewritten function how the
        function understands which form to process, as it doesn't appear to specify
        the scope, i.e. document.form.e lement, or am I missing something?

        Many thanks!


        "kaeli" <tiny_one@NOSPA M.comcast.net> wrote in message
        news:MPG.1b5dbe 28420233b9989f8 2@nntp.lucent.c om...[color=blue]
        > In article <foGdnVdG36MJZ2 7dRVn-sQ@eclipse.net. uk>, remove_this_ash @a-
        > hall.com enlightened us with...[color=green]
        > > Example of how I'm using this within the HTML itself...
        > >
        > > <input name="LinkCommi ssionPercentage " type="text"
        > > class="GenericF ormTextBox" id="LinkCommiss ionPercentage" value="<% If
        > > TheLinkCommissi onPercentage <> "" Then %><%= TheLinkCommissi onPercentage
        > > %><% Else %>0<% End If %>" size="5" maxlength="4"
        > >[/color][/color]
        OnChange="Ash_C heckCommission( 'formaddproduct ','LinkCommissi onPercentage',' L[color=blue][color=green]
        > > inkCommissionFe e');">
        > >
        > > Cheers, Ash!
        > >[/color]
        >
        > Ewww, get all those evals out of there. Icky. ;)
        >
        > OnChange="Ash_C heckCommission( this, this.form.eleme nts
        > ['LinkCommission Fee'])"
        >
        > function Ash_CheckCommis sion (TheFieldToChec k,TheFieldToDis able)
        > {
        > if (TheFieldToChec k.value > 0 &&
        > TheFieldToDisab le.value > 0)
        > {
        > alert('You may only enter a Commission Percentage OR Commission
        > fee, not both.');
        > TheFieldToDisab le.value="0";
        > }
        > }
        >
        > Okay, now here's the thing - you're comparing a value to a number and
        > setting a value to a string. Either it's a number or it's a string. Pick
        > one.
        > If it's a number, change the value comparisons above to use parseInt (or
        > parseFloat) and set the value to 0, not "0". If it is a string, change
        > the comparitors to strings. (0 to "0")
        > Considering the field names, I'll assume they are supposed to be floats.
        >
        > if (parseFloat(The FieldToCheck.va lue,10) > 0 &&
        > parseFloat(TheF ieldToDisable.v alue,10) > 0)
        >
        > TheFieldToDisab le.value=0;
        >
        > Warning: if the user might enter something that is not a number, check
        > before using parseFloat. I recommend using regular expressions.
        >
        > HTH
        >
        > --
        > --
        > ~kaeli~
        > The best part of having kids is giving them back to their
        > parents.
        > http://www.ipwebdesign.net/wildAtHeart
        > http://www.ipwebdesign.net/kaelisSpace
        >[/color]


        Comment

        • Andrew Thompson

          #5
          Re: Javascript Validation Function aint workin ;o(

          On Tue, 13 Jul 2004 16:53:17 +0100, J. Hall wrote:
          [color=blue]
          > Thanks for your quick and detailed response! I'm a little confused being a
          > VB person, not Javascript and haven't yet come across the term 'Float', new
          > territory![/color]

          Float is a common term in many computing
          languages, including Java and C#.

          This page compares the C# 'float', to VB's
          'Single' data type..
          <http://www.harding.edu/USER/fmccown/WWW/vbnet_csharp_co mparison.html#d atatypes>
          [ a floating point number ]

          ...unless Kaeli was referring to CSS 'float',
          which is a positioning value.. (shrugs)

          --
          Andrew Thompson
          http://www.PhySci.org/ Open-source software suite
          http://www.PhySci.org/codes/ Web & IT Help
          http://www.1point1C.org/ Science & Technology

          Comment

          • kaeli

            #6
            Re: Javascript Validation Function aint workin ;o(

            In article <6v2dnSuFvdvtmG ndRVn-ug@eclipse.net. uk>, remove_this_ash @a-
            hall.com enlightened us with...[color=blue]
            > Thanks for your quick and detailed response! I'm a little confused being a
            > VB person, not Javascript and haven't yet come across the term 'Float', new
            > territory![/color]

            Float is a floating point numeric value, as opposed to an integer.
            123.234 is a float, as is 12.32, 0.34 and so on. 123 is an integer.
            (The 10 was the base; not required, but recommended.)

            VB in ASP.net also has a float data type.


            us/cpref/html/frlrfSystemSing leClassParseTop ic1.asp

            [color=blue]
            >
            > I don't quite understand though from your rewritten function how the
            > function understands which form to process, as it doesn't appear to specify
            > the scope, i.e. document.form.e lement, or am I missing something?
            >[/color]

            It is passing the object itself rather than a string that represents the
            name of the object. (which is why you needed eval in yours, b/c you were
            just passing a string that represented the name of an object.)

            Think of it like this: (VB-ish)
            Public Void myFunc (Object formElement, Object formElement2)

            Now, the formElement object knows which form it belongs to, so you could
            do
            formElement.for m and it would know which form you meant. But if you just
            want its value, it knows that, too.

            An object has attributes. One of the attributes of a form element object
            is which form it belongs to. Others include its name, its ID, its type,
            and its value. The form object knows what document it belongs to, and
            the document knows which window it belongs to. It's all about objects.
            If you haven't done any real object oriented programming, it might help
            you to read a bit about that.

            Does that help?

            --
            --
            ~kaeli~
            Murphy's Law #2000: If enough data is collected, anything
            may be proven by statistical methods.



            Comment

            • Richard Cornford

              #7
              Re: Javascript Validation Function aint workin ;o(

              kaeli wrote:
              <snip>[color=blue]
              > It is passing the object itself rather than a string that
              > represents the name of the object. (which is why you needed
              > eval in yours, b/c you were just passing a string that
              > represented the name of an object.)[/color]
              <snip>

              Resolving the constructed dot notation property accessors created using
              the passed names was what the - eval - was being used for, but - eval -
              was not *needed* as the passed names could have been used in bracket
              notation property accessors instead. (Though it certainly is better to
              be passing the object references to the function instead of control
              names.)

              Richard.


              Comment

              • kaeli

                #8
                Re: Javascript Validation Function aint workin ;o(

                In article <cd19nb$adk$1$8 302bc10@news.de mon.co.uk>,
                Richard@litotes .demon.co.uk enlightened us with...[color=blue]
                > kaeli wrote:
                > <snip>[color=green]
                > > It is passing the object itself rather than a string that
                > > represents the name of the object. (which is why you needed
                > > eval in yours, b/c you were just passing a string that
                > > represented the name of an object.)[/color]
                > <snip>
                >
                > Resolving the constructed dot notation property accessors created using
                > the passed names was what the - eval - was being used for, but - eval -
                > was not *needed* as the passed names could have been used in bracket
                > notation property accessors instead. (Though it certainly is better to
                > be passing the object references to the function instead of control
                > names.)
                >[/color]

                This is true. 'Needed' was a bad word choice on my part.

                I never claimed to have a really wide vocabulary or be good at phrasing
                things, now did I? ;)

                --
                --
                ~kaeli~
                A bicycle can't stand on its own because it is two tired.



                Comment

                • Mick White

                  #9
                  Re: Javascript Validation Function aint workin ;o(

                  kaeli wrote:
                  [color=blue]
                  > In article <6v2dnSuFvdvtmG ndRVn-ug@eclipse.net. uk>, remove_this_ash @a-
                  > hall.com enlightened us with...
                  >[color=green]
                  >>Thanks for your quick and detailed response! I'm a little confused being a
                  >>VB person, not Javascript and haven't yet come across the term 'Float', new
                  >>territory![/color]
                  >
                  >
                  > Float is a floating point numeric value, as opposed to an integer.
                  > 123.234 is a float, as is 12.32, 0.34 and so on. 123 is an integer.
                  > (The 10 was the base; not required, but recommended.)[/color]



                  But parseFloat doesn't take a second parameter, though.

                  Mick[color=blue]
                  >
                  >
                  >[color=green]
                  >>I don't quite understand though from your rewritten function how the
                  >>function understands which form to process, as it doesn't appear to specify
                  >>the scope, i.e. document.form.e lement, or am I missing something?
                  >>[/color]
                  >
                  >
                  > It is passing the object itself rather than a string that represents the
                  > name of the object. (which is why you needed eval in yours, b/c you were
                  > just passing a string that represented the name of an object.)
                  >
                  > Think of it like this: (VB-ish)
                  > Public Void myFunc (Object formElement, Object formElement2)
                  >
                  > Now, the formElement object knows which form it belongs to, so you could
                  > do
                  > formElement.for m and it would know which form you meant. But if you just
                  > want its value, it knows that, too.
                  >
                  > An object has attributes. One of the attributes of a form element object
                  > is which form it belongs to. Others include its name, its ID, its type,
                  > and its value. The form object knows what document it belongs to, and
                  > the document knows which window it belongs to. It's all about objects.
                  > If you haven't done any real object oriented programming, it might help
                  > you to read a bit about that.
                  >
                  > Does that help?
                  >[/color]

                  Comment

                  • kaeli

                    #10
                    Re: Javascript Validation Function aint workin ;o(

                    In article <0yWIc.44323$yd 5.21900@twister .nyroc.rr.com>, mwhite13
                    @BOGUSrochester .rr.com enlightened us with...[color=blue]
                    >
                    >
                    >
                    > But parseFloat doesn't take a second parameter, though.
                    >[/color]

                    Whoops. My bad. parseInt takes a radix. parseFloat does not.


                    --
                    --
                    ~kaeli~
                    Suicide is the most sincere form of self-criticism.



                    Comment

                    Working...