Javascript Validation Function aint workin ;o(

J

J. Hall

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

J. Hall

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!
 
K

kaeli

remove_this_ash@a- said:
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

J. Hall

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 said:
remove_this_ash@a- said:
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
 
K

kaeli

remove_this_ash@a- said:
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
 
R

Richard Cornford

kaeli wrote:
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.
 
K

kaeli

kaeli wrote:

<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? ;)

--
 
M

Mick White

kaeli said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top