filling a form with dates...newbie needs a bit of help

W

WindAndWaves

Dr John Stockton said:
JRS: In article <[email protected]>, dated Wed, 15
Dec 2004 21:14:42, seen in WindAndWaves
In the function, which Rob and you really helped me a lot - thank you again
for that. We have a date (from a datepicker), to which we add a couple of
days only (lets say 30 at the very very most). Because time does not come
into it, I dont think that date light saving matters, but correct me if I am
wrong, I may have misunderstood you.Are you saying that, for some people,
their local javascript engine may take daylight saving into account so when
we start adding a few days it may end up being before midnight rather than
on midnight? In that case, daylight saving obviously does matter. Could
you clarify that (I do want to get it right, but I prefer to keep it as
simple as pos.)

Summer time can matter, even if you are only trying to add one day, if
you do it by adding 86400 seconds. Where there is summer time, one
Autumn day is 90000 seconds long (except for Lord Howe Island).

For subtracting days, note that one Spring day (e f L H I) can be 82800
seconds long.

Javascript ignores Leap Seconds.

In javascript, getTime(), valueOf(), and new Date(x) where x is a
number, use milliseconds GMT (UTC, if you prefer).

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
||||||||||||||||||||||||||||||||||||||||||||||||||


<FAQENTRY> An entry referring to dates should mention the importance of
discriminating between GMT, local Winter Time, & local Summer Time.
links.

I know fully appreciate the problem.
 
W

WindAndWaves

Still trying to comprehend it all. Still one question, does the line below
mean that gapNights takes on the value of the gField?

At first, with my limited programming background, i assumed it to be only a
check IF they were the same.

if ( gField && !(gapNights = gField.value) || !checkDigit(gapNights)) {

Sorry to ask such a lame question, just curious.

- Nicolaas
 
L

Laurent Bugnion

Hi,
Still trying to comprehend it all. Still one question, does the line below
mean that gapNights takes on the value of the gField?

At first, with my limited programming background, i assumed it to be only a
check IF they were the same.

if ( gField && !(gapNights = gField.value) || !checkDigit(gapNights)) {

Sorry to ask such a lame question, just curious.

- Nicolaas

The assignment operator '=' assigns to the left hand value the content
of the right hand value. The comparison operator '==' compares two
values and returns true if both are equivalent.

The definition of "equivalent" can be different depending on the type,
so it has sometimes unexpected effects, but mostly you can rely on this.

Note that JavaScript will go the economical way and evaluate the
expression "!(gapNights = gField.value)" only if gField is true. The
reason is that the result of "!(gapNights = gField.value)" doesn't
matter is gField is null, because the result of the && operation is
always false. This is what allows to check gField.value (it will never
be evaluated if gField is null) without other precautions (evaluating
gField.value causes an exception if gField is null). However, in this
case, it has the side effect that gapNights never receives the value of
gField.value if gField is null. This is why assignments should not be
done this way, but it was not your intent anyway.

HTH,

Laurent
 
W

WindAndWaves

Laurent Bugnion said:
Hi,


The assignment operator '=' assigns to the left hand value the content
of the right hand value. The comparison operator '==' compares two
values and returns true if both are equivalent.

The definition of "equivalent" can be different depending on the type,
so it has sometimes unexpected effects, but mostly you can rely on this.

Note that JavaScript will go the economical way and evaluate the
expression "!(gapNights = gField.value)" only if gField is true. The
reason is that the result of "!(gapNights = gField.value)" doesn't
matter is gField is null, because the result of the && operation is
always false. This is what allows to check gField.value (it will never
be evaluated if gField is null) without other precautions (evaluating
gField.value causes an exception if gField is null). However, in this
case, it has the side effect that gapNights never receives the value of
gField.value if gField is null. This is why assignments should not be
done this way, but it was not your intent anyway.

Thank you, that makes more sense now.

- Nicolaas
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 16
Dec 2004 14:01:05, seen in WindAndWaves
Lines: 67

I know fully appreciate the problem.

Since you refuse to comply with Usenet conventions, I assume that you
will not want any further assistance.
 
W

WindAndWaves

Dr John Stockton said:
Since you refuse to comply with Usenet conventions, I assume that you
will not want any further assistance.

Dear John

I am sorry - your assumption is wrong, I do really appreciate your
assistance, I am just not very newsgroup savvy yet. I did notice your thing
about quoting signatures, but only after I sent my reply. At least if that
is what you are referring to.

Thank you once more for your help and guiding me in becoming a better
newsgroup user.

Kind regards


- Nicolaas
 
R

RobG

WindAndWaves wrote:
[...]
Thank you, that makes more sense now.

- Nicolaas

I posted a reply to Dr. J that seems to have never made it to the page.
It included some updates, below is just the last script, including most
of his recommendations.

I've dropped a lot of comments to reduce the size.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>Visit Properties</TITLE>
<script type="text/javascript">
// Make month and day arrays global
var monthsA = ['Jan','Feb','Mar','Apr',
'May','Jun','Jul','Aug',
'Sep','Oct','Nov','Dec'];
var daysA = ['Sun','Mon','Tue','Wed',
'Thu','Fri','Sat'];

function formatDate(d) {
var s = daysA[d.getDay()]
+ ' ' + d.getDate()
+ ' ' + monthsA[d.getMonth()]
+ ' ' + d.getFullYear();
return s;
}

function calc(f){

// Setup variables - msg is for debug
var ele, x, n, numNights, gapNights,
depDate, nField, gField;
var digitErr = 'You must enter a whole number from zero to 99';

// Substitute doc.all function to allow for older IEs
if (document.all && !document.getElementById) {
document.getElementById = function(id) {
return document.all[id] }
}
ele = f.elements;

// Check month adjustment, keep aligned with input
var aa = f.elements['startdate'].value.split('-');
// var aa = f.elements['startdate'].value.split(/\D+/);
var sd = new Date(aa[2],aa[1]-1,aa[0]);

for (var i=0; i<ele.length; i++) {
if (ele.name) {
if (ele.name.split('-')[0] == 'v') {
// Get the element id extension
n = ele.name.split('-')[1];
if (ele.checked) {
document.getElementById('a-' + n).innerHTML
= formatDate(sd);
nField = f.elements['n-' + n];
if ( !(numNights = nField.value)
|| !checkDigit2(numNights)) {
alert(digitErr);
// Blank field
nField.value = '';
// Put focus on it if you like
nField.focus();
return false;
}
// Add the number of nights to get depature date
sd.setDate(sd.getDate() + +numNights);
// Write dep date to form
document.getElementById('d-' + n).innerHTML
= formatDate(sd);
// Get gap nights
gField = f.elements['g-' + n];
if ( gField
&& !(gapNights = gField.value)
|| !checkDigit(gapNights)) {
alert(digitErr);
gField.value = '';
gField.focus();
return false;
}
sd.setDate(sd.getDate() + +gapNights);
} else {
document.getElementById('a-' + n).innerHTML = '---';
f.elements['n-' + n].value = '';
document.getElementById('d-' + n).innerHTML = '---';
if (f.elements['g-' + n])
f.elements['g-' + n].value = '';
}
}
}
}
}

function checkDigit(x) {
var dayRegX = /^\d{1,2}$/;
// var dayRegX = /^(0|[1-9])\d*$/

// It's easier to check 1/2 digit fields with a RegExp -
// OK = /^\d?\d$/.test(field)
// and if OK it can safely be converted to Number with a unary +.

if (!dayRegX.test(x)) {
return false;
}
return true;
}

function checkDigit2(x) {
return x == parseInt(x,10) && x == parseFloat(x);
}
</script>

<style type="text/css">
body, th, td {font-family: sans-serif; font-size: 90%;}
th {background-color: #dfdfdf; padding: 0 8 0 8;}
td {text-align: center;}
..sg {width: 10em;}
..xs {width: 2em;}
..hint {color: #333366; font-weight: normal; font-size: 90%;}
..dateP, .dateQ, dateO
{position: absolute; left:0px; top:0px; width: 100px; height: 100px;
background-color: blue; border: thin solid red; z-index: 100;}
..dateQ
{background-color: red; left: 0px; top: 0px; z-index: 50;}
..dateO
{position: relative; display: inline;}
</style>

</HEAD>
<BODY>
<FORM METHOD="POST" NAME="basket" ID="basket" action="">
<!-- This field is just to provide a start date -->

<label for="startdate">Start Date
<input type="text" value="05-12-2004" name="startdate"></label>
<table border="0" cellspacing="0">
<thead>
<tr>
<th>Property Name</th>
<th style="width: 10em;">Will you visit<br>this
property?</th>
<th style="width: 12em;">Date of arrival<br><span
class="hint">after 1pm</span></th>
<th>How many nights<br>you will stay?</th>
<th style="width: 12em;">Date of departure<br><span
class="hint">before 10am</span></th>
<th>Nights gap to<br>next property</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3">
<INPUT TYPE="reset" VALUE="reset" onclick="this.blur();"
ID="Reset1" NAME="Reset1"><br>
</td>
<td colspan="3">
<INPUT TYPE="button" VALUE="calculate" ONCLICK="
this.blur();
return calc(this.form);
"
ID="calculate" NAME="calculate" tabindex="8">
</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>property 1</td>
<td><input type="checkbox" id="v-1" name="v-1" checked></td>
<td><span class="sg" ID="a-1">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="1" ID="n-1" NAME="n-1"
tabindex="1"></td>
<td><span class="sg" ID="d-1">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="0" ID="g-1" NAME="g-1"
tabindex="2"></td>
</tr>
<tr>
<td>property 2</td>
<td><input type="checkbox" id="v-2" name="v-2" checked></td>
<td><span class="sg" ID="a-2">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="1" ID="n-2" NAME="n-2"
tabindex="3"></td>
<td><span class="sg" ID="d-2">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="0" ID="g-2" NAME="g-2"
tabindex="4"></td>
</tr>
<tr>
<td>property 3</td>
<td><input type="checkbox" id="v-3" name="v-3" checked></td>
<td><span class="sg" ID="a-3">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="1" ID="n-3" NAME="n-3"
tabindex="5"></td>
<td><span class="sg" ID="d-3">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="0" ID="g-3" NAME="g-3"
tabindex="6"></td>
</tr>
<tr>
<td>property 4</td>
<td><input type="checkbox" id="v-4" name="v-4" checked></td>
<td><span class="sg" ID="a-4">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="1" ID="n-4" NAME="n-4"
tabindex="7"></td>
<td><span class="sg" ID="d-4">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="0" ID="g-4" NAME="g-4"
style="display: none;"></td>
</tr>
</tbody>
</table>
</form>
</BODY>
</HTML>
 
W

WindAndWaves

RobG said:
WindAndWaves wrote:
[...]
Thank you, that makes more sense now.

- Nicolaas

I posted a reply to Dr. J that seems to have never made it to the page.
It included some updates, below is just the last script, including most
of his recommendations.


I am just not quite sure how I can thank you. It has just been awesome - it
was hard for me to even keep up with all the reading. Once the final
product it up, I will post a link so you can see the final results.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 16
Dec 2004 23:48:35, seen in RobG
function checkDigit2(x) {
return x == parseInt(x,10) && x == parseFloat(x);
}


What's that meant for?

The latest code has both checkDigit and checkDigit2; and spends more
effort on checking than appears necessary.

St = thing.value
OK = /^\d?\d$/.test(St)
if (!OK) { /* set focus, return false */ }
Numeric = +St

A string consisting of an optional digit followed by a compulsory digit
MUST be in 0..0, 00..99, and MUST convert to an appropriate number under
the action of a unary + operator. NO other string should AIUI be
permitted in this application.

<URL:http://www.merlyn.demon.co.uk/js-valid.htm> etc.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 18 Dec 2004
06:15:34, seen in Ravichandran J. V.
"You're not in America - the word is "cannot", which has a different
meaning to "can not"."

How does this matter?

Precision of meaning is *always* important. From your name, you are
Indian ; educated Indians have always had a reputation for precision in
their use of the English language. Emulate.

You are posting, know it or not, to Usenet; please respect accepted
conventions for the number of lines in a signature.
 
W

WindAndWaves

Dr John Stockton said:
You are posting, know it or not, to Usenet; please respect accepted
conventions for the number of lines in a signature.
[...]


Dear John,

Two little comments:

1. It is probably useless to tell everyone about the news group etiquette as
it would be like a bumper sticker saying: "Please respect the rules of the
road". Most people know the rules but they do not always adhere to them for
one reason or another. We all agree with you that net etiquette is
important, but, like language, it used to be rude to say "fucking rad dude",
but these days this language seems to be common place among young people.
Like language, the net is a living thing, hard to control.

b. We are all still dying to know the difference between can not and cannot.
I totally agree with your note that language precision is very important...

Anyway, I have a lot of respect for your knowledge and I also wanted to
thank you for your input.

With warm regards from Aotearoa (formerly known as New Zealand)

- Nicolaas
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sun, 19
Dec 2004 14:32:35, seen in WindAndWaves
1. It is probably useless to tell everyone about the news group etiquette as
it would be like a bumper sticker saying: "Please respect the rules of the
road". Most people know the rules but they do not always adhere to them for
one reason or another.

They need to know that if they do not comply with what the regulars like
then they will receive fewer, perhaps far fewer, answers from the
regulars. They may not like that, but it is so. Usually, anyway, they
are people insufficiently well-informed to understand the purpose served
by various parts of etiquette. You are, it seems, an example.
 

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,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top