473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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_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

Jul 23 '05 #1
9 1490
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" <re*************@a-hall.com> wrote in message
news:sp********************@eclipse.net.uk...
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

Jul 23 '05 #2
In article <fo********************@eclipse.net.uk>, 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

Jul 23 '05 #3
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" <ti******@NOSPAM.comcast.net> wrote in message
news:MP************************@nntp.lucent.com...
In article <fo********************@eclipse.net.uk>, 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

Jul 23 '05 #4
On Tue, 13 Jul 2004 16:53:17 +0100, J. Hall wrote:
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 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_comparison.html#datatypes>
[ 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
Jul 23 '05 #5
In article <6v********************@eclipse.net.uk>, 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

Jul 23 '05 #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.
Jul 23 '05 #7
In article <cd*******************@news.demon.co.uk>,
Ri*****@litotes.demon.co.uk 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

Jul 23 '05 #8
kaeli wrote:
In article <6v********************@eclipse.net.uk>, 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?

Jul 23 '05 #9
In article <0y*******************@twister.nyroc.rr.com>, 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

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
2
by: GIMME | last post by:
Background ... I've created a web application that allows a user to create an HTML application from IE. The application itself creates an XML representation of a XHTML form. The XHTML...
7
by: mhk | last post by:
Hi, Is there any way to create/set Session veriable in JavaScript. Please let me know if anyone has an idea. Thanks alot.
2
by: Chris Barrow | last post by:
Hi everyone, Does anyone know if it is possible to append your own javascript function onto the end of a button's onclick method when validation controls are used? Here is an example of the...
2
by: Fourge | last post by:
Hi, I have run into a very strange scenario. In developing an ASP.NET application on framework version 1.1, I found that certain client-side validation scripts were not being rendered. The...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
5
by: Peter Afonin | last post by:
Hello, I'm not an expert in Javascript, so I'm seeking an advice. As I mentioned in my previous post, I use Javascript to check whether at least one checkbox in the datagrid has been checked....
2
by: goscottie | last post by:
I need to find a way to either 1. run and check all Validation controls (in my case one CompareValidator) and run client side javascript function. In this case I'll use <asp:Button>. So if all...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.