Exceptionhandling in statemachine

J

Joergen Vig

Hi,
I'm going to create one of the classic checks in Web-development : THE
DATE CHECKING !

I have made some kind of statemachine for datechecking, but I don't
know where to return the 0 (zero) value, for date-checking succeeded :

void function TestDate() {
i = 1;
dd = parseInt(document.forms(0).txtDateringDay.value);
yy = parseInt(document.forms(0).txtDateringYear.value);
mm = parseInt(document.forms(0).txtDateringMonth.value);
while (i > 0)
switch (i) {
case 1 :
if (dd < 1 || dd > 31) {
errmsg = "Day value is not accepted";
i = -1;
}
else {
i++;
}
break;
case 2 :
if (mm < 1 || mm > 12) {
errmsg = "Month value is not accepted";
i = -1;
}
else {
i++;
}
break;
case 3 :
//----HERE is the problem, I think :
try{
CurrDate = new date(mm,dd,yy);
}
catch (e) {
errmsg = "Date format is not accepted";
i = -1;
{
break;

}
}
}
if (i < 0) {
alert("Error : " + errmsg);
}
else {
alert("Date is accepted!");
}
}


Thanks,

Joergen Jensen
 
L

Lasse Reichstein Nielsen

I'm going to create one of the classic checks in Web-development : THE
DATE CHECKING !

Why? :)
I have made some kind of statemachine for datechecking,

Is this for a programming course, because I can't see any other reason
for doing a state machine for three things that has to happen in sequence.
Normal program flow wil handle that.
but I don't know where to return the 0 (zero) value, for
date-checking succeeded :

There us a much simpler way using the built in Date object.
void function TestDate() {
i = 1;
dd = parseInt(document.forms(0).txtDateringDay.value);

You should give parseInt a second argument of 10, otherwise, a user
writing "012" will be surpriced to find it meaning 10 (interpreted as
octal).

Are you sure you want an input of "12 bulliwug" to count as 12?
yy = parseInt(document.forms(0).txtDateringYear.value);
mm = parseInt(document.forms(0).txtDateringMonth.value);

Here you just do:
---
var d = new Date(yy,mm-1,dd);
if (d.getMonth()+1 != mm || d.getDate() != dd) {
alert("Date does not exist");
return 1;
}

It also handles illegal dates like 31th of february.
//----HERE is the problem, I think :
try{
CurrDate = new date(mm,dd,yy);

Date is with a capital D.

It never throws an exception. And it counts months from 0=January to
11=December.

/L
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top