Solved

Posted on 2000-01-18
Medium Priority
297 Views
For the form at http://www.geocities.com/hutton_l/exchange/sbcs.html I need a JavaScript alert for the field "Date of Birth / Baptism."

The correct format is 01JAN1900 - two numbers, three letters, four numbers.

I need an alert that will accept only the following:

Two numbers:
any two-digit value between 01 and 31

Three letters:
JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV or DEC

Four numbers:
any four-digit value starting with 1
0
Question by:lhutton
• 8
• 6
• 2

LVL 8

Expert Comment

ID: 2365951
Hi there:

here goes the script:

--------- 8< --------- 8< -----
<html>
<title>Example</title>
<script>
<!--
months = new arr("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC")
function arr() {
this.length=arr.arguments.length
for (n=0;n<arr.arguments.length;n++) this[n] = arr.arguments[n]
}
function isNumber (tmp) {   var i;
for (i=0;i<tmp.length;i++) {
c = tmp.charAt(i)
if (c < "0" || c > "9") return false
}
return true
}
function isMonth(month_tmp) {
month_tmp = month_tmp.toUpperCase()
for (i=0; i<12; i++) {
if (months[i] == month_tmp) return true
}
return false
}
function checkday() {
day_tmp = document.formulario.myday.value
myday  = day_tmp.substring(0,2)
mymonth = day_tmp.substring(2,5)
myyear = day_tmp.substring(5,9)
if (!isNumber(myday) || myday<"01" || myday > "31") {
return false
}
if (!isMonth(mymonth)) {
return false
}
if (!isNumber(myyear) || myyear<"1000" || myyear > "1999") {
return false
}
return true
}
-->
</script>
<body>
<form name="formulario">
DD/MM/YYYY:<input type="text" name="myday"><br>
<input type="button" value="Test" onclick="checkday()">
</form>
</body>
</html>
-------- 8< ------- 8< -----
xabi
0

LVL 75

Expert Comment

ID: 2366620
No-one born in 2000???

Michel
0

LVL 8

Expert Comment

ID: 2366661
mplungjan:

yes, but you must read the question:

>YEAR:
>Four numbers:
any four-digit value starting with 1

ok?

xabi
0

LVL 75

Expert Comment

ID: 2366830
Xabi: I asked Leith, sorry if it came across as a criticism of your script - not the intention...
0

LVL 8

Expert Comment

ID: 2366903
No, it didn't sound like that, don't worry.

See ya Michel

xabi
0

Author Comment

ID: 2368491
Sorry, my mistake - 2000 is possible too :-)

I've uploaded the changes to http://www.geocities.com/hutton_l/dev/sacs.html and just want to check a couple of things...

I changed myday to Date_of_Event_______ which is the name of the date field and formulario to myForm which is the name of the form. I don't know whether this has any ill effect, although it does seem to be working.

I thought the line
month_tmp = month_tmp.toUpperCase()
might force the month entered to upper case but it doesn't. Is it supposed to, or do I need to keep the line
onBlur="this.value=this.value.toUpperCase();"

Instead of a button, I've got
onBlur="checkday()"
in the date field.

Please let me know whether there's anything I need to correct and what I should do about the upper case thing.
0

LVL 8

Accepted Solution

xabi earned 300 total points
ID: 2369876
Hi there:

CHange the checkday function into this one:

function checkday() {
day_tmp = document.theForm.Date_of_Event_______.value
myday = day_tmp.substring(0,2)
mymonth = day_tmp.substring(2,5)
myyear = day_tmp.substring(5,9)
if (!isNumber(myday) || myday < "00" || myday > "31" || myday.length != 2) {
return false
}
if (!isMonth(mymonth)) {
return false
}
if (!isNumber(myyear) || myyear < "1700" || myyear > "2000" || year.length != 4) {
return false
}
}

Continue using myday instead of Date_of_Event_______ cause "Date_of_Event_______" is the name of the form field for the whole date.

There is no problem about changing the name of the form.

xabi
0

Author Comment

ID: 2371556
Thanks for that. I noticed you added a check specifying the number of digits has to be 2 and 4 respectively. Am using onBlur to force to uppercase and have uploaded changes to http://www.geocities.com/hutton_l/dev/sacs.html

One other thing - if the date field is empty, I don't want any error to come up when someone clicks out of it; only when they enter an invalid value. Is this possible?

Thanks
0

Author Comment

ID: 2372036
I just noticed a major problem that only seems to appear in a frameset. If a user clicks in the date field then tries to scroll the page or click out of it, the user gets stuck in an endless number of alerts saying the field is invalid. Check at http://www.geocities.com/hutton_l/exchange/sbcs.html

It doesn't happen out of the frameset at http://www.geocities.com/hutton_l/dev/sacs.html
0

LVL 8

Expert Comment

ID: 2373468
Here goes the code:

------- 8< -------- 8< -------
function checkday() {
day_tmp = document.theForm.Date_of_Event_______.value
if (day_tmp.length != 9) {
myday = day_tmp.substring(0,2)
mymonth = day_tmp.substring(2,5)
myyear = day_tmp.substring(5,9)
if (!isNumber(myday) || myday < "00" || myday > "31" || myday.length != 2) {
return false
}
if (!isMonth(mymonth)) {
return false
}
if (!isNumber(myyear) || myyear < "1700" || myyear > "2000" || year.length != 4) {
return false
}
}
}
--------- 8< --------- 8< -----

if date length is not 9 then it doesn't perform the check.

xabi
0

Author Comment

ID: 2373817
Uploaded changes to http://www.geocities.com/hutton_l/exchange/sbcs.html but still have problem of getting stuck in the error :-(
0

LVL 8

Expert Comment

ID: 2373867
Hi there:

You are right.

1st.- I made a mistake and you must change:
if (day_tmp.length != 9) {
into
if (day_tmp.length == 9) {
Sorry.

2nd.- This only happens with NS not with IE.

3th.- It's not easy to solve cand I'll try to explain you why:
When you click outside the field (Scroll, etc) function checkday is called.
This function checks the day and gives an error (Alert window)
And NS thinks that he must call again the function "onblur" cause you ckicked on the alert window
So as you can see you enter in an infinite loop.

I suggest you to make the check of the day on submit time, not on onblur time.

xabi
0

Author Comment

ID: 2375419
Changing
if (day_tmp.length != 9) {
to
if (day_tmp.length == 9) {
disables the script. No alerts are produced.

Further, changing
onBlur
to
onSubmit
has no effect on the date field at all.

Surely there must be some way to prevent the loop in NS???
0

Author Comment

ID: 2375474
I just noticed that whatever value of 9 characters I enter now it accepts. ie. it accepts 51JAN4000 as valid! All I want is for it to stop looping. Have reverted back to code given at Thursday, January 20 2000 - 07:44PM NZDT.
0

Author Comment

ID: 2377868
If a user clicks in the date field then tries to scroll the page or click out of the field, they get stuck in an infinite loop of alerts saying the field is invalid.

This problem occurs in NC 4.7 but not in IE 5.0 and appears to arise only in a frameset.

The problem file is http://www.geocities.com/hutton_l/exchange/sbcs.html

The script works fine out of the frameset at http://www.geocities.com/hutton_l/dev/sacs.html

if (day_tmp.length != 9) {
to the checkday function allows any value of nine characters to be entered and renders all other checks useless.

Using onSubmit in place of onBlur prevents the script from having any effect.

I need to resolve this otherwise as I cannot run the risk of people getting themselves stuck in a flood of errors, should they happen  to scroll the page or click out of the field.
0

Author Comment

ID: 2379906
OK, will accept your answer. But credit to Michel for pointing out that onChange should be used in place of onBlur to prevent the infinite loop problem. Thanks :-)
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.