Javascript Validation Function aint workin ;o(

Discussion in 'Javascript' started by J. Hall, Jul 13, 2004.

  1. J. Hall

    J. Hall Guest

    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_CheckCommission(TheFormToCheck,TheFieldToCheck,TheFieldToDisable) {
    if (eval('document.'+TheFormToCheck+'.'+TheFieldToCheck+'.value > 0 &&
    document.'+TheFormToCheck+'.'+TheFieldToDisable+'.value > 0')) {
    alert('You may only enter a Commission Percentage OR Commission Fee, not
    both.');
    eval('document.'TheFormToCheck+'.'+TheFieldToDisable+'.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, Jul 13, 2004
    #1
    1. Advertising

  2. J. Hall

    J. Hall Guest

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

    <input name="LinkCommissionPercentage" type="text"
    class="GenericFormTextBox" id="LinkCommissionPercentage" value="<% If
    TheLinkCommissionPercentage <> "" Then %><%= TheLinkCommissionPercentage
    %><% Else %>0<% End If %>" size="5" maxlength="4"
    OnChange="Ash_CheckCommission('formaddproduct','LinkCommissionPercentage','L
    inkCommissionFee');">

    Cheers, Ash!



    "J. Hall" <> wrote in message
    news:...
    > 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_CheckCommission(TheFormToCheck,TheFieldToCheck,TheFieldToDisable) {
    > if (eval('document.'+TheFormToCheck+'.'+TheFieldToCheck+'.value > 0 &&
    > document.'+TheFormToCheck+'.'+TheFieldToDisable+'.value > 0')) {
    > alert('You may only enter a Commission Percentage OR Commission Fee, not
    > both.');
    > eval('document.'TheFormToCheck+'.'+TheFieldToDisable+'.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, Jul 13, 2004
    #2
    1. Advertising

  3. J. Hall

    kaeli Guest

    In article <>, remove_this_ash@a-
    hall.com enlightened us with...
    > Example of how I'm using this within the HTML itself...
    >
    > <input name="LinkCommissionPercentage" type="text"
    > class="GenericFormTextBox" id="LinkCommissionPercentage" value="<% If
    > TheLinkCommissionPercentage <> "" Then %><%= TheLinkCommissionPercentage
    > %><% Else %>0<% End If %>" size="5" maxlength="4"
    > OnChange="Ash_CheckCommission('formaddproduct','LinkCommissionPercentage','L
    > inkCommissionFee');">
    >
    > Cheers, Ash!
    >


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

    OnChange="Ash_CheckCommission(this, this.form.elements
    ['LinkCommissionFee'])"

    function Ash_CheckCommission (TheFieldToCheck,TheFieldToDisable)
    {
    if (TheFieldToCheck.value > 0 &&
    TheFieldToDisable.value > 0)
    {
    alert('You may only enter a Commission Percentage OR Commission
    fee, not both.');
    TheFieldToDisable.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(TheFieldToCheck.value,10) > 0 &&
    parseFloat(TheFieldToDisable.value,10) > 0)

    TheFieldToDisable.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
    kaeli, Jul 13, 2004
    #3
  4. J. Hall

    J. Hall Guest

    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.element, or am I missing something?

    Many thanks!


    "kaeli" <> wrote in message
    news:...
    > In article <>, remove_this_ash@a-
    > hall.com enlightened us with...
    > > Example of how I'm using this within the HTML itself...
    > >
    > > <input name="LinkCommissionPercentage" type="text"
    > > class="GenericFormTextBox" id="LinkCommissionPercentage" value="<% If
    > > TheLinkCommissionPercentage <> "" Then %><%= TheLinkCommissionPercentage
    > > %><% Else %>0<% End If %>" size="5" maxlength="4"
    > >

    OnChange="Ash_CheckCommission('formaddproduct','LinkCommissionPercentage','L
    > > inkCommissionFee');">
    > >
    > > Cheers, Ash!
    > >

    >
    > Ewww, get all those evals out of there. Icky. ;)
    >
    > OnChange="Ash_CheckCommission(this, this.form.elements
    > ['LinkCommissionFee'])"
    >
    > function Ash_CheckCommission (TheFieldToCheck,TheFieldToDisable)
    > {
    > if (TheFieldToCheck.value > 0 &&
    > TheFieldToDisable.value > 0)
    > {
    > alert('You may only enter a Commission Percentage OR Commission
    > fee, not both.');
    > TheFieldToDisable.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(TheFieldToCheck.value,10) > 0 &&
    > parseFloat(TheFieldToDisable.value,10) > 0)
    >
    > TheFieldToDisable.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
    >
    J. Hall, Jul 13, 2004
    #4
  5. J. Hall

    kaeli Guest

    In article <>, remove_this_ash@a-
    hall.com enlightened us with...
    > 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!


    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.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-
    us/cpref/html/frlrfSystemSingleClassParseTopic1.asp


    >
    > 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.element, or am I missing something?
    >


    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.form 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.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, Jul 13, 2004
    #5
  6. kaeli wrote:
    <snip>
    > 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.)

    <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.
    Richard Cornford, Jul 13, 2004
    #6
  7. J. Hall

    kaeli Guest

    In article <cd19nb$adk$1$>,
    enlightened us with...
    > kaeli wrote:
    > <snip>
    > > 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.)

    > <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.)
    >


    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.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, Jul 13, 2004
    #7
  8. J. Hall

    Mick White Guest

    kaeli wrote:

    > In article <>, remove_this_ash@a-
    > hall.com enlightened us with...
    >
    >>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!

    >
    >
    > 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.)




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

    Mick
    >
    >
    >
    >>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.element, or am I missing something?
    >>

    >
    >
    > 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.form 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?
    >
    Mick White, Jul 13, 2004
    #8
  9. J. Hall

    kaeli Guest

    kaeli, Jul 14, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Benson
    Replies:
    10
    Views:
    459
    Dennis Lee Bieber
    Jan 21, 2004
  2. Bob
    Replies:
    8
    Views:
    84
    Erik Max Francis
    Sep 22, 2003
  3. windandwaves
    Replies:
    4
    Views:
    148
  4. Îίκος Αλεξόπουλος

    Cookie aint retrieving when visiting happens from a backlink.

    Îίκος Αλεξόπουλος, Oct 25, 2013, in forum: Python
    Replies:
    26
    Views:
    191
    Nick the Gr33k
    Oct 27, 2013
  5. Îίκος Αλεξόπουλος

    Cookie aint retrieving when visiting happens from a backlink.

    Îίκος Αλεξόπουλος, Oct 25, 2013, in forum: Python
    Replies:
    1
    Views:
    91
    Denis McMahon
    Oct 25, 2013
Loading...

Share This Page