By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,797 Members | 1,191 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,797 IT Pros & Developers. It's quick & easy.

strange error

P: n/a
Hi Gurus

The page below has a strange error. It seems to be working very well, just
when you enter 8 or 9 for day, month or year then you get an error. I
really have no idea where that is coming from. Can you help?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Online availability</TITLE>
<META NAME="keywords" CONTENT="Online availability" >
<META NAME="description" CONTENT="Online availability" >
<META NAME="Author" CONTENT="www.sunnysideup.co.nz" >
<SCRIPT TYPE="text/javascript">
<!--
function padLeft(str, pad, count) {
while(str.length<count)
str=pad+str;
return str;
}

function check(el, field) {
//controller
var result={error:""};
for(var ii=2; ii<arguments.length; ii++)
if((result=singleCheck(el, field, arguments[ii])).error)
break;
return result;

//single check
function singleCheck(el, field, type) {
var result={error:""};
switch(type) {
case "empty" :
if(/^\s*$/.test(el.value)) {
result.error="field "+field+" : should not be empty.";
}
break;
case "2num" :
if(!/^\d+$/.test(el.value)) {
result.error="field "+field+" : should contain only numbers.";
} else {
el.value=
padLeft(el.value.replace(/^\d+(\d\d)$/,"$1"), "0", 2);
}
break;
}

if(el.style)
el.style.color = result.error ? "#c00" : "";

return result;
}
}

function validate(form){
var a=[], msg=[], index=1;

// re-test the fields
a[a.length]=check(form.elements[0], "foo1", "empty", "2num");
a[a.length]=check(form.elements[1], "foo2", "empty", "2num");
a[a.length]=check(form.elements[2], "foo3", "empty", "2num");

//analyse the tests
for(var ii=0; ii<a.length; ii++)
if(a[ii].error)
msg[msg.length]=(index++)+" - "+a[ii].error;

//alert the message, if any
if(msg.length)
alert(msg.join("\n"));

//handle form's submission
return !msg.length;
}

function valid(form) {
var e = 0

var field = form.year;
var useryear = parseInt(field.value);
if (!useryear) {
alert("You must indicate a year (e.g. 04 or 05).");
field.focus();
field.select();
e = e + 1
} else if (useryear >= 4) {

} else {
alert("You should enter a year in the future, for example 05, you
entered " + useryear);
field.focus();
field.select();
e = e + 1
}

var field = form.month;
var usermonth = parseInt(field.value);
if (!usermonth) {
alert("You must indicate a month (e.g. 12 or 03).");
field.focus();
field.select();
e = e + 1
} else if (usermonth <= 12) {

} else {
alert("You should enter a month between 01 and 12, you entered " +
usermonth);
field.focus();
field.select();
e = e + 1
}

var field = form.day;
var userday = parseInt(field.value);
if (!userday) {
alert("You must indicate a day (e.g. 10 or 03).");
field.focus();
field.select();
e = e + 1
} else if (userday <= 31) {

} else {
alert("You should enter a day between 01 and 31, you entered " +
userday);
field.focus();
field.select();
e = e + 1
}
document.location.href='#' + form.day.value + form.month.value +
form.year.value;
}
-->

</SCRIPT>
<STYLE TYPE="text/css">
BODY, TABLE, TD, H1 {font-family: verdana, arial, helvetica, tahoma,
geneva;}
DIV.content { }
TABLE{border: 3px solid #CCCCCC; WIDTH: 406px; font-size: 10px;
margin-top: 100px; margin-bottom: 1200px;}

/*search table */
H1 {padding: 5px; text-align: center; background-color: #FFCF1B;
font-size: 12px; margin: 0px; color: white;}
TD.W400H20E8c {width: 400px; height: 20px; background-color: #E8E8E8;
text-align: center;}
TD.W50H40E8r {width: 50px; height: 40px; text-align:
right;background-color: #e8e8e8;}
TD.W400H40E8c {width: 400px; Height: 40px; background-color: #CCCCCC;
text-align: center;}
TD.W450H40E8 {width: 450px; height: 40px; background-color: #e8e8e8;}
input.date {width: 20px; height: 20px; border: 1px solid #FFCF1B;}
input.go {width: 400px; height: 20px; border: 1px solid #FFCF1B;}

/*data table */
TD.f {Height: 20px; background-color: #E8E8E8;}
TD.c {Height: 20px; background-color: #E8E8E8; text-align: center;}
TD.h {Height: 20px; background-color: #FFCF1B; text-align: center;}
</STYLE>
</HEAD>
<BODY STYLE="background-color: #FFFFFF"><DIV ALIGN=CENTER CLASs=content>
<FORM>
<TABLE CELLSPACING=0 CELLPADDING=0 CLASS=s>
<TR><Td COLSPAN=2 class=W400H20E8c><H1>SEARCH FOR AVAILABILITY - ENTER
DATE</H1></TD></TR>
<TR><TD CLASS=W50H40E8r><Input value=00 type=text name="day" SIZE=1
onblur="check(this, 'foo1', 'empty', '2num')" CLASS=date></TD><TD
CLASS=W450H40E8>&nbsp;&nbsp;day (e.g. 02 or 23)</TD></TR>
<TR><TD CLASS=W50H40E8r><Input value=00 type=text name="month" SIZE=1
onblur="check(this, 'foo1', 'empty', '2num')" CLASS=date></TD><TD
CLASS=W450H40E8>&nbsp;&nbsp;month (e.g. 03 or 12)</TD></TR>
<TR><TD CLASS=W50H40E8r><Input value=00 type=text name="year" SIZE=1
onblur="check(this, 'foo1', 'empty', '2num')" CLASS=date></TD><TD
CLASS=W450H40E8>&nbsp;&nbsp;year (e.g. 04 or 05)</TD></TR>
<TR><TD COLSPAN=2 class=W400H20E8c><INPUT TYPE="button" VALUE="SEARCH"
onClick="return valid(form)" CLASS=go></TD></TR>
</TABLE>
</FORM>
<TABLE CELLSPACING=0 CELLPADDING=5 CLASS=d BORDER=1>
<TR><TD class=h>Day</TD><TD class=h>Room 1</TD><TD class=h>Room
2</TD></TR>
<TR><TD class=f><A NAME=110305></A>11 March 2005</TD><TD
class=c>3</TD><TD class=c>3</TD></TR>
<TR><TD class=f><A NAME=120305></A>12 March 2005</TD><TD
class=c>3</TD><TD class=c>3</TD></TR>
<TR><TD class=f><A NAME=130305></A>13 March 2005</TD><TD
class=c>3</TD><TD class=c>3</TD></TR>
<TR><TD class=f><A NAME=140305></A>14 March 2005</TD><TD
class=c>3</TD><TD class=c>3</TD></TR>
<TR><TD class=f><A NAME=150305></A>15 March 2005</TD><TD
class=c>3</TD><TD class=c>3</TD></TR>
<TR><TD class=f><A NAME=160305></A>16 March 2005</TD><TD
class=c>3</TD><TD class=c>3</TD></TR>
<TR><TD class=f><A NAME=170305></A>17 March 2005</TD><TD
class=c>3</TD><TD class=c>3</TD></TR>
</TABLE>
<BR>last updated: 11 November 2004, 13:50
</BODY>
</HTML>
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
WindAndWaves wrote:
The page below has a strange error. It seems to be working very well, just
when you enter 8 or 9 for day, month or year then you get an error. var useryear = parseInt(field.value);


parseInt can actually take two arguments; the first one is the string to
be converted to an integer (with specific conversion rules), and the
second one (which is optional) is the base to be used. If you omit the
second argument, then the base is guessed by the function: strings
starting by "0x" are hexadecimal, strings starting by "0" are octal and
the rest is decimal.

In your case, since your numbers are starting with a "0", they're
assumed to be octal - and in an octal base "08" or "09" do not exist.
You therefore need to force the base:

parseInt(field.value, 10);

Also, don't forget to test the format of the string with the regexps
before actually calling parseInt!

<URL:http://jibbering.com/faq/#FAQ4_12>
<URL:http://www.jibbering.com/faq/faq_notes/type_convert.html#tcPrIntRx>
Regards,
Yep.
Jul 23 '05 #2

P: n/a
Lee
WindAndWaves said:

Hi Gurus

The page below has a strange error. It seems to be working very well, just
when you enter 8 or 9 for day, month or year then you get an error.


No need to read further.
Wherever you're using parseInt(), add the radix argument:

parseInt(whatever.value,10);

Numbers with leading zeros are assumed to be octal, unless you tell
parseInt to treat them as base 10.

08 and 09 are invalid octal values.

Jul 23 '05 #3

P: n/a

"Lee" <RE**************@cox.net> wrote in message
news:cm*********@drn.newsguy.com...
WindAndWaves said:

Hi Gurus

The page below has a strange error. It seems to be working very well, justwhen you enter 8 or 9 for day, month or year then you get an error.


No need to read further.
Wherever you're using parseInt(), add the radix argument:

parseInt(whatever.value,10);

Numbers with leading zeros are assumed to be octal, unless you tell
parseInt to treat them as base 10.

08 and 09 are invalid octal values.

THANK YOU
Jul 23 '05 #4

P: n/a

"Yann-Erwan Perio" <y-*******@em-lyon.com> wrote in message
news:41**********************@news.free.fr...
WindAndWaves wrote:
The page below has a strange error. It seems to be working very well, just when you enter 8 or 9 for day, month or year then you get an error.

var useryear = parseInt(field.value);


parseInt can actually take two arguments; the first one is the string to
be converted to an integer (with specific conversion rules), and the
second one (which is optional) is the base to be used. If you omit the
second argument, then the base is guessed by the function: strings
starting by "0x" are hexadecimal, strings starting by "0" are octal and
the rest is decimal.

In your case, since your numbers are starting with a "0", they're
assumed to be octal - and in an octal base "08" or "09" do not exist.
You therefore need to force the base:

parseInt(field.value, 10);

Also, don't forget to test the format of the string with the regexps
before actually calling parseInt!

<URL:http://jibbering.com/faq/#FAQ4_12>
<URL:http://www.jibbering.com/faq/faq_notes/type_convert.html#tcPrIntRx>
Regards,
Yep.

THANK YOU
Jul 23 '05 #5

P: n/a
JRS: In article <AO******************@news.xtra.co.nz>, dated Sun, 31
Oct 2004 10:35:13, seen in news:comp.lang.javascript, WindAndWaves
<ac****@ngaru.com> posted :
The page below has a strange error. It seems to be working very well, just
when you enter 8 or 9 for day, month or year then you get an error. I
really have no idea where that is coming from. Can you help?


Did we not already advise you to read the newsgroup FAQ?

<FAQENTRY> FAQ 4.12 needs a link to FAQ 4.21 </FAQENTRY>

There seems no point in demanding, or even allowing, leading zeroes in
Y M D if they are entered as separate fields. If the output needs them,
the code should provide them.

It is more logical to have the fields in Y M D order; the current
settings of Y M can then reasonably be used to set an appropriate upper
limit on D.

Four-digit years are always better; for the present, you could put 20
before the box.

Function parseInt(X, 10) is only needed (AFAIR) if X may contain non-
whitespace after the number.

You use "numbers" to mean "digits". '05' and '23' are numbers, but are
not digits.

It's not clear to me whether the year should be in the future, or the
date; apparently you allow anything after 2004-01-01.

When posting code to News, you must not let your system break lines. If
you cannot control your posting agent, you must make *all* of your lines
sufficiently short.

You presumably should check that the date is valid, and not such as Feb
30 or Jun 31 - see FAQ, section 3.2, and below.

It accepts a month of -3, because you test for !=0 and <=12.

Parts are repetitive; use a common function, with parameters.

You don't need such detailed error messages; put "future year" or
"future date" on the form.

The code is very much longer than is needed to enter, and validate, a
future date. Remember that it all has to be downloaded by the user.

I'd use a single field, asking for entries as "Y M D", numeric, e.g.
2004 6 12; pattern-validate and split with a RegExp using \D+ for
separators; put into a Date Object and check M & D for value validation,
check against new Date() or something calculated from that for date
range validity ("future"), and use finally

document.location.href='#' + (Y*100+M+1)*100+D

where Y M D are the validated numbers.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6

P: n/a

"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:m5**************@merlyn.demon.co.uk...
JRS: In article <AO******************@news.xtra.co.nz>, dated Sun, 31
Oct 2004 10:35:13, seen in news:comp.lang.javascript, WindAndWaves
<ac****@ngaru.com> posted :
The page below has a strange error. It seems to be working very well, justwhen you enter 8 or 9 for day, month or year then you get an error. I
really have no idea where that is coming from. Can you help?
Did we not already advise you to read the newsgroup FAQ?

<FAQENTRY> FAQ 4.12 needs a link to FAQ 4.21 </FAQENTRY>

There seems no point in demanding, or even allowing, leading zeroes in
Y M D if they are entered as separate fields. If the output needs them,
the code should provide them.

It is more logical to have the fields in Y M D order; the current
settings of Y M can then reasonably be used to set an appropriate upper
limit on D.

Four-digit years are always better; for the present, you could put 20
before the box.

Function parseInt(X, 10) is only needed (AFAIR) if X may contain non-
whitespace after the number.

You use "numbers" to mean "digits". '05' and '23' are numbers, but are
not digits.

It's not clear to me whether the year should be in the future, or the
date; apparently you allow anything after 2004-01-01.

When posting code to News, you must not let your system break lines. If
you cannot control your posting agent, you must make *all* of your lines
sufficiently short.

You presumably should check that the date is valid, and not such as Feb
30 or Jun 31 - see FAQ, section 3.2, and below.

It accepts a month of -3, because you test for !=0 and <=12.

Parts are repetitive; use a common function, with parameters.

You don't need such detailed error messages; put "future year" or
"future date" on the form.

The code is very much longer than is needed to enter, and validate, a
future date. Remember that it all has to be downloaded by the user.

I'd use a single field, asking for entries as "Y M D", numeric, e.g.
2004 6 12; pattern-validate and split with a RegExp using \D+ for
separators; put into a Date Object and check M & D for value validation,
check against new Date() or something calculated from that for date
range validity ("future"), and use finally

document.location.href='#' + (Y*100+M+1)*100+D

where Y M D are the validated numbers.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE

4 <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,

links.
Dear John

Thank you for your comments.

Let me note:
a. I am really useless with javascript. I am a complete novice.
b. your idea of one field is great, but I would like to make it easy for my
users.
c. if i knew how to use javascript then I would write something simple, but
I have no idea where to start.
d. I would follow up on all your comments if only i knew how to.
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.