-1

ive written a html form that allows a user to select their birthday from a drop down menu. Once the user submits the form those values are then stored into variables in a javascript function that will will first check that the user selected a value from each field(month, day year) and then verify the birthday is a valid date. If everything is correct then the next form is suppose to load. My problem is that once the function verifies that a month and day has been selected the program loads the next form without checking the year.

Here is the java script function:

function checkBday() {
  var day = document.forms["register"]["DateOfBirth_Day"].value;
  var month = document.forms["register"]["DateOfBirth_Month"].value;
  var year = document.forms["register"]["DateOfBirth_Year"].value;
  if(month == "- Month -") {
    alert("Select a month");
    return false;
  }
  if(day == "- Day -") {
    alert("Select a day");
    return false;
  }
  if(year == "- year -") {
    alert("Select a year");
    return false;
  }
  if((month == 2 && day > 28) || (month == 4 && day == 31) || (month == 6 && day == 31) || (month == 9 && day == 31) || (month == 11 && day == 31)) {
    alert("Invalid birthday");
    return false;
  }
}

Here is the function call:

if(checkBday() == false) {
    return false; }
else {
    alert("Registration was successful!");
    return true; }

- Day- - Month - and - Year- are default values if nothing is selected from the drop down menu. Thanks for the help im a noob at this.

Elmer
  • 809
  • 9
  • 15
terence vaughn
  • 521
  • 2
  • 11
  • 23
  • 2
    Do you have any error in your JS? Take a look at the console. Also include the markup for these select elements. The reason why I ask for markup is because your "y" in year is lower case, while in the rest is upper case. You even write it upper case at the end. – Hanlet Escaño Jul 11 '14 at 21:19
  • 2
    Odd code. checkBday never returns true. Only undefined if no "if" is triggered. You need to provide your HTML too. – iCollect.it Ltd Jul 11 '14 at 21:19
  • if(!month) { alert("Select a month"); return false; } – user1572796 Jul 11 '14 at 21:20
  • `undefined` is `falthy`. When is your function returning `true`? – PM 77-1 Jul 11 '14 at 21:20
  • String comparison is case sensitive. Try capital year as in `"- Year -"` – mtmk Jul 11 '14 at 21:23
  • 1
    `month == 2 && day > 28` – what about leap years? See [*How to validate a date*](http://stackoverflow.com/questions/5812220/how-to-validate-a-date/5812341#5812341). – RobG Jul 11 '14 at 21:26
  • Thanks Hanlet it was because of the lower case 'y'!!!! I guess ive been staring at this program waaaaaay to long. – terence vaughn Jul 11 '14 at 21:27
  • RobG I have been giving that some thought im not really sure how to account for a leap, or how to verify a past leap year. Any suggestion? – terence vaughn Jul 11 '14 at 21:29
  • @terencevaughn—you can also check the selected index, it should be 1 or greater (-1 means no option is selected, 0 is first), then you can make the value of the first option anything you like and don't have to change your code. – RobG Jul 11 '14 at 21:30
  • Thanks so much! Also off the subject could anyone tell me why i got a negative vote for this question? I just got a message from the site saying that my question wasn't well received and that im in danger of being blocked unless I revise it. im totally lost. im not sure what I have done wrong. – terence vaughn Jul 12 '14 at 02:49

3 Answers3

1

I would refactor your code to look something like this:

function checkBday() {
  var day = document.forms["register"]["DateOfBirth_Day"].value;
  var month = document.forms["register"]["DateOfBirth_Month"].value;
  var year = document.forms["register"]["DateOfBirth_Year"].value;

  var default_month = "- Month -";
  var default_day = "- Day -";
  var default_year = "- Year -"
  // Validate presence of all fields.
  if (month == default_month) {
    alert("Select a month");
    return false;
  } else if (day == default_day) {
    alert("Select a day");
    return false;
  } else if (year == default_year) {
    alert("Select a year");
    return false;
  }

  // Validate date.
  if((month == 2 && day > 28) || (month == 4 && day == 31) || (month == 6 && day == 31) || (month == 9 && day == 31) || (month == 11 && day == 31)) {
    alert("Invalid birthday");
    return false;
  } else {
    return true;
  }
}

Also, I believe there might have been a typo in your code. You are comparing the year to "- year -". Where the year is not capitalized("- Year -"). Maybe this was the problem in addition to the function not returning anything.

Elmer
  • 809
  • 9
  • 15
1

It is coming back as undefined because you dont give a return value if the condition falls all the way through the nested values.

Take your "else" statement and put it before the closing bracket in the actual function, like this:

function checkBday() {
var day = document.forms["register"]["DateOfBirth_Day"].value;
var month = document.forms["register"]["DateOfBirth_Month"].value;
var year = document.forms["register"]["DateOfBirth_Year"].value;
if(month == "- Month -") {
    alert("Select a month");
    return false; }
    if(day == "- Day -") {
        alert("Select a day");
        return false; }
    if(year == "- year -") {
        alert("Select a year");
        return false; }
    if((month == 2 && day > 28) || (month == 4 && day == 31) || (month == 6 && day == 31) || (month == 9 && day == 31) || (month == 11 && day == 31)) {
                    alert("Invalid birthday");
                    return false;
                }
alert("Registration was successful!");
return true;            
        }
Edward
  • 79
  • 8
0

create a new variable called registration_successful. If variable is true after running checkBday, proceed with next form.

  function checkBday() {
          var registration_succesfull = false;
          var day = document.forms["register"]["DateOfBirth_Day"].value;
          var month = document.forms["register"]["DateOfBirth_Month"].value;
          var year = document.forms["register"]["DateOfBirth_Year"].value;
      if(month == "- Month -") {
           alert("Select a month");
           registration_succesfull = false; }
      else if (day == "- Day -") {
        alert("Select a day");
        registration_succesfull = false; }
      else if (year == "- year -") {
        alert("Select a year");
        registration_succesfull = false; }
      else if((month == 2 && day > 28) || (month == 4 && day == 31) || (month == 6 && day == 31) || (month == 9 && day == 31) || (month == 11 && day == 31)) {
                    alert("Invalid birthday");
                    registration_succesfull = false;  }

       else {registration_succesfull = true; } 
  return registration_succesfull;
       }

note: february can have 29 days as well

Maps Ylno
  • 33
  • 6