JavaScript Alert Needed

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
lhuttonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xabiCommented:
Hi there:

here goes the script:

--------- 8< --------- 8< -----
<html>
<head>
<title>Example</title>
</head>
<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") {
    alert("Day is invalid")
    return false
  }
  if (!isMonth(mymonth)) {
    alert("Month is invalid")
    return false
  }
  if (!isNumber(myyear) || myyear<"1000" || myyear > "1999") {
    alert("Year is invalid")
    return false
  }
  alert("all ok")
  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
Michel PlungjanIT ExpertCommented:
No-one born in 2000???

Michel
0
xabiCommented:
mplungjan:

yes, but you must read the question:
He is asking for:

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

ok?

xabi
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Michel PlungjanIT ExpertCommented:
Xabi: I asked Leith, sorry if it came across as a criticism of your script - not the intention...
0
xabiCommented:
No, it didn't sound like that, don't worry.

See ya Michel

xabi
0
lhuttonAuthor Commented:
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
xabiCommented:
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) {
        alert("Day is invalid; please check the Help tool")
        return false
      }
      if (!isMonth(mymonth)) {
        alert("Month is invalid; please check the Help tool")
        return false
      }
      if (!isNumber(myyear) || myyear < "1700" || myyear > "2000" || year.length != 4) {
        alert("Year is invalid; please check the Help tool")
        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.

No problem about onblur.

xabi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lhuttonAuthor Commented:
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
lhuttonAuthor Commented:
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
xabiCommented:
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) {
   alert("Day is invalid; please check the Help tool")
   return false
  }
  if (!isMonth(mymonth)) {
   alert("Month is invalid; please check the Help tool")
   return false
  }
  if (!isNumber(myyear) || myyear < "1700" || myyear > "2000" || year.length != 4) {
   alert("Year is invalid; please check the Help tool")
   return false
  }
 }
}
--------- 8< --------- 8< -----

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

xabi
0
lhuttonAuthor Commented:
Uploaded changes to http://www.geocities.com/hutton_l/exchange/sbcs.html but still have problem of getting stuck in the error :-(
0
xabiCommented:
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
lhuttonAuthor Commented:
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
lhuttonAuthor Commented:
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
lhuttonAuthor Commented:
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

Adding the line
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
lhuttonAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.