Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Javascript Validation Function aint workin ;o(

Reply
Thread Tools

Javascript Validation Function aint workin ;o(

 
 
J. Hall
Guest
Posts: n/a
 
      07-13-2004
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+'.'+TheFieldToChe ck+'.value > 0 &&
document.'+TheFormToCheck+'.'+TheFieldToDisable+'. value > 0')) {
alert('You may only enter a Commission Percentage OR Commission Fee, not
both.');
eval('document.'TheFormToCheck+'.'+TheFieldToDisab le+'.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



 
Reply With Quote
 
 
 
 
J. Hall
Guest
Posts: n/a
 
      07-13-2004
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','Li nkCommissionPercentage','L
inkCommissionFee');">

Cheers, Ash!



"J. Hall" <> wrote in message
news:spSdna-utrl-a27dRVn-...
> 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+'.'+TheFieldToChe ck+'.value > 0 &&
> document.'+TheFormToCheck+'.'+TheFieldToDisable+'. value > 0')) {
> alert('You may only enter a Commission Percentage OR Commission Fee, not
> both.');
> eval('document.'TheFormToCheck+'.'+TheFieldToDisab le+'.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
>
>
>



 
Reply With Quote
 
 
 
 
kaeli
Guest
Posts: n/a
 
      07-13-2004
In article <foGdnVdG36MJZ27dRVn->, 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','Li nkCommissionPercentage','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

 
Reply With Quote
 
J. Hall
Guest
Posts: n/a
 
      07-13-2004
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 <foGdnVdG36MJZ27dRVn->, 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','Li nkCommissionPercentage','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
>



 
Reply With Quote
 
kaeli
Guest
Posts: n/a
 
      07-13-2004
In article <6v2dnSuFvdvtmGndRVn->, 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/de...l=/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

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      07-13-2004
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.


 
Reply With Quote
 
kaeli
Guest
Posts: n/a
 
      07-13-2004
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

 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      07-13-2004
kaeli wrote:

> In article <6v2dnSuFvdvtmGndRVn->, 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?
>

 
Reply With Quote
 
kaeli
Guest
Posts: n/a
 
      07-14-2004
In article <0yWIc.44323$>, mwhite13
@BOGUSrochester.rr.com enlightened us with...
>
>
>
> But parseFloat doesn't take a second parameter, though.
>


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


--
--
~kaeli~
Suicide is the most sincere form of self-criticism.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
urlib.quote gives KeyError in Python 2.4.4 but workin 2.3.5 nyenyec Python 3 01-07-2007 07:34 PM
using nested for loop, workin with displaying prime numbers triplejump24 C++ 3 11-10-2006 05:23 PM
Wireless stopped workin stainless Wireless Networking 2 09-01-2006 03:59 PM
Impersonating a User and Starting Standalone Processes stop workin =?Utf-8?B?SW5kZXB0aA==?= ASP .Net 1 04-01-2005 09:05 PM
Scandisk and Defrag not workin' ...? Morph Computer Support 2 02-12-2005 10:44 PM



Advertisments