Consistently inconsistent error regarding the nature of a control -how do I fix this?

T

Ted Byers

Here is the error:
'edate.value' is null or not an object

I get at this using the debugger presented by MS IE.

Here is how the page/form definition begins:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Campaign Definition</title>
</head>
<body>
<h1>Campaign Configuration</h1>
<br>
<form name='campaign' id='campaign'
action='cgi-bin/create_definition.cgi' method=POST
onSubmit="return verify_data(this)">
<table>

And here are two controls that are used in precisely the same way (at
least on the web page - the differences play out in the perl script
and DB on the server):
<tr>
<td>Campaign start date
</td>
<td>
<input type=text id='sdate' name='sdate' size=10 maxlength=10 />
(format yyyy-mm-dd)
</td>
</tr>
<tr>
<td>Campaign end date
</td>
<td>
<input type=text id='edate' name='edate' size=10 maxlength=10 />
(format yyyy-mm-dd)
</td>
</tr>

And finally, here are the Javascript functions that are in use here
(with the statement where the script dies clearly flagged):
<SCRIPT LANGUAGE="Javascript" type="text/javascript">
function test_date(x) {
var pattern = /\d\d\d\d-\d\d-\d\d/;
if (x.length != 10) return false;
return pattern.test(x);
}
function test_time(x) {
var pattern = /\d\d:\d\d:\d\d/;
if (x.length != 8) return false;
return pattern.test(x);
}
function test_nws(x) {
var pattern = /\w+/;
return !pattern.test(x);
}
function verify_data(f) {
var f1 = test_nws(f.mname.value);
var f2 = test_nws(f.cname.value);
var f3 = test_nws(f.sdate.value);
var f4 = test_nws(f.edate.value);
var f5 = test_nws(f.stime.value);
var f6 = test_nws(f.etime.value);
var f;
var msg = "";
if (f1) {
msg += "You must provide a valid merchant name.\n";
}
if (f2) {
msg += "You must provide a valid campaign name.\n";
}
if (!f3) {
f = test_date(f.sdate.value);
if (!f) {
msg += "The start date provided does not have a recognized
format.\n";
}
} else {
msg += "You must provide a valid start date for your
campaign.\n";
}
if (!f4) {
***********************************************************************************************************
the following statement is where the script apparently dies with the
error: "'edate.value' is null or not an object"
***********************************************************************************************************
f = test_date(f.edate.value);
if (!f) {
msg += "The end date provided does not have a recognized
format.\n";
}
} else {
msg += "You must provide a valid end date for your campaign.
\n";
}
if (!f5) {
f = test_time(f.stime.value);
if (!f) {
msg += "The start time provided does not have a recognized
format.\n";
}
}
if (!f6) {
f = test_time(f.etime.value);
if (!f) {
msg += "The start time provided does not have a recognized
format.\n";
}
}
if (msg) {
alert(msg);
return false;
}
return true;
}
</SCRIPT>

Now the CONSISTENT behaviour is that all the functions used with the
input control sdate work as expected, giving the expected boolean
results for each condition checked for along with the correct contents
for msg.

Further, if edate is empty, I get the correct error message. But when
edate has a value, I get the error message "'edate.value' is null or
not an object".

The inconsistency is that sdate and edate are created and used in
exactly the same way.

It is clear that test_nws is working correctly. It is also clear,
from every condition I test for regarding dates, that test_date works
correctly on sdate. It is likely that test_time works, but I can't
say for certain because the script aborts before getting to it.

Fortunately, the cgi script does nothing at present except echo the
data back to the user in a different format (so I could confirm it was
getting the correct data), and IT seems to be getting whatever data I
give to edate.

I would have expected that if my Javascript or my HTML had an error
related to my checking dates, it would affect both sdate and edate,
but I am baffled as to why only edate is affected.

Any ideas?

Thanks

Ted
 
R

Richard Cornford

On Aug 24, 5:18 pm, Ted Byers wrote:
<form name='campaign' id='campaign'
action='cgi-bin/create_definition.cgi' method=POST
onSubmit="return verify_data(this)">
function verify_data(f) {
var f1 = test_nws(f.mname.value);
var f2 = test_nws(f.cname.value);
var f3 = test_nws(f.sdate.value);
var f4 = test_nws(f.edate.value);
var f5 = test_nws(f.stime.value);
var f6 = test_nws(f.etime.value);
var f;
var msg = "";
if (f1) {
msg += "You must provide a valid merchant name.\n";
}
if (f2) {
msg += "You must provide a valid campaign name.\n";
}
if (!f3) {
f = test_date(f.sdate.value);

If you change the value of - f - here it will no longer be a reference
to a form object when you next attempt to reference form controls as
named properties of - f -.
if (!f) {
msg += "The start date provided does not have a
recognized format.\n";
}
} else {
msg += "You must provide a valid start date for your
campaign.\n";
}
if (!f4) {
***************************************************************
the following statement is where the script apparently dies with
the error: "'edate.value' is null or not an object"
***************************************************************
f = test_date(f.edate.value);
I would have expected that if my Javascript or my HTML had an
error related to my checking dates, it would affect both sdate
and edate, but I am baffled as to why only edate is affected.

That will be because it is your last use of - f.sdate - that assigns
to - f -.

Richard.
 
T

Ted Byers

On Aug 24, 5:18 pm, Ted Byers wrote:
<snip>





If you change the value of - f - here it will no longer be a reference
to a form object when you next attempt to reference form controls as
named properties of - f -.







That will be because it is your last use of - f.sdate - that assigns
to - f -.

Richard.

Thanks Richard,

And thanks for not pointing out what an idiot I am for allowing such a
name collision.

Thanks

Ted
 
G

Garrett Smith

Richard said:
On Aug 24, 5:18 pm, Ted Byers wrote:

// use the elements collection, e.g.
// var els = f.elements;
// els.mname.value;

// Problem: Don't redeclare a formal parameter value.
You'll just confuse yourself (and others) that way.

It's a good idea to use the - elements - collection, which has standard
behavior and is widely supported.

You should also validate the HTML. Drop the closing "/" -- those show up
in validator errors. Drop the 'LANGUAGE=javascript' and why use upper
case for your script tag?

Garrett
 
D

Dr J R Stockton

In comp.lang.javascript message <d78b52da-bd51-4255-8cc4-1be7166dd476@c2
9g2000yqd.googlegroups.com>, Mon, 24 Aug 2009 09:18:21, Ted Byers
<SCRIPT LANGUAGE="Javascript" type="text/javascript"> ^^^^ not wanted ^^^^
function test_date(x) {
var pattern = /\d\d\d\d-\d\d-\d\d/;
if (x.length != 10) return false;
return pattern.test(x);
}
function test_time(x) {
var pattern = /\d\d:\d\d:\d\d/;
if (x.length != 8) return false;
return pattern.test(x);
}

If you use ^ and $ at the ends of your patterns, the separate length
test will not be needed.

It would be easy enough to validate the date, so that the mistake of
using dates like April 31 could be trapped. See via sig below.

The time can be largely validated by using /^[0-2]\d:[0-5]\d:[0-5]\d$/
and with a little more effort the hours can be checked for <=23.

With date-and-time that accurate, you should consider whether seasonal
clock shifts might matter,
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top