Link to home
Start Free TrialLog in
Avatar of Bette Lamore
Bette LamoreFlag for United States of America

asked on

JS form validation-- a glitch

I have all the variables declared, yet in the interest of time suffice it to say that bDay--Month--
Year represents birthday month, day, year and that the variables monthday,month and year were derived from the object date so that they are the current day, month and year. I have a form with a submit button that runs a validation script. All works well including the following "if" statement:

<code>if (bYear > (year - 16))
      {
      alert("If year is entered correctly, driver is under 16 years old and further
documentation will be required to manually override system validation.");
      form_xydl.bYear.focus();
      return false;
      }</code>      
      
until I get to the following:
 
<code>
if ((bYear == (year-16)) && (bMonth > month))
      {
      alert("If year is entered correctly, driver is under 16 years old and further documentation will be required to manually override system validation.");
      form_xydl.bYear.focus();
      return false;
      }</code>      
I am getting all days with the same bMonth as month as an alert, even though I put that the bMonth had to be GREATER than the month -- not greater or EQUAL TO. Why is this?? It is driving me crazy and I have tried all solutions to no avail. My goal is to combine the above if statement (once I get it to allow monthdays in the same birth month as the current month to validate OK) to read

<code>if (((bYear == (year-16)) && (bMonth > month)) || ((bYear == (year-16)) && (bMonth == month) && (bDay > monthday)))
      {
      alert("If year is entered correctly, driver is under 16 years old and further
documentation will be required to manually override system validation.");
      form_xydl.bYear.focus();
      return false;
      }</code>      
      
which would mean that if the person would be 16 the current year and month yet his birthday had not come up yet, he should have an alert that he would need further documentation before getting his drivers license before his 16th birthday.
Thanks for any clarification anyone can offer. I have been trying every which way but loose to come up with a work-around -- even created an AGE function to return age so that I could simply put
<code>if (age<16)
{alert......
Avatar of GregArnott
GregArnott
Flag of Australia image

If this line leads to >= for the month:
if ((bYear == (year-16)) && (bMonth > month)) 

Open in new window


Then change it to:
if ((bYear == (year-16)) && (bMonth > month -1)) 

Open in new window


I feel there is an issue with how you are determining your month. Not sure. I do know that (-1) the value will create a quick fix for you.
Avatar of Michel Plungjan
JS months start at 0 for Jan
Avatar of Bette Lamore

ASKER

It sounded good, Greg, yet when I tried it I still get validation error for when birth month - current month yet birthday has passed. Thank you for your effort :-)
Atached is my html and my validation script if this helps. webPage.htm validation.js
Line 4 of js file:
var bMonth              = document.form_xydl.bMonth.value;

Open in new window

Change to:
var bMonth              = document.form_xydl.bMonth.value - 1;

Open in new window


Line 13 of js file:
var year                = now.getYear();

Open in new window

Change to:
var year                = now.getFullYear();

Open in new window


As mplungjan correctly mentioned, JavaScript months are 0-11, and this fixes it.
Year was not showing the appropriate four digit value, and this update fixes that.

Also, line 104 of html file:
<<label for="r1">blue</label>

Open in new window

To:
<label for="r1">blue</label>

Open in new window

I appreciate your effort yet your "fix" actually breaks my code for the month validation. If you had run the code the way I had it originally, Greg, you would have seen that the code works for the correct year and the month the way I wrote it -- no problem -- the only problem was with the statement that  bmonth == month -- the script showed false for ALL days in Oct when the bmonth was Oct -- not just for the birthdays that were in the future. I tidied up the code last night to put the "ifs" in the order that the form had them and caught my typo -- thanks for noticing too.
Here is my revised tidied up code without your solution. Enter bday as 10-30-95 or actually any day in Oct and you will get the message that the driver is under 16 --- if you enter 9 for bmonth, you will not get that message -- with your code it invalidates for 9 as well so in this case the month - 1 solution does not work. My problem was never with the month -- only with the days when the bmonth equals the current month for the year when the driver would be 16 years old. I gues the best way for you to see my problem is simply to run the script and enter the drivers bday as 10-3-1995 and you will see what I am talking about. here is my tidied up scripts and I attached your script as well so you can see how it breaks my validation.
I really appreciate your attempts -- they just aren't working in this case. validation.js Gregvalidation.js webPage.htm
It was when I ran your scripts all the way through step by step in the browser, that the problem line in your code was showing current year as '111'.

My response was simply highlighting these errors. Sorry, ran out of time yesterday to be able to contribute more than that.

I'll take these files and come up with working code.
Thanks, Greg -- no rush :-)
Appreciate your help!
Bette
ASKER CERTIFIED SOLUTION
Avatar of GregArnott
GregArnott
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I should also add some tips for debugging code like this.

Run Firefox with the Firebug plugin, and step through the JavaScript validation as it processes your input.

Alternatively, Chrome's inbuilt developer tools (hit F12) is fantastic.

Between these two browsers and the information that I acquire from these panes, I manage to see and correct most issues.
wow, Greg, YOU DID IT!!!! Thanks so much!!
Yes I have used Firefox in the past -- Firebug -- and the new version came out and I have been lazy to learn the new interface. A little different from the old version. I will do so in the future as I can see the value of it.
By, you really earned your points on this one!!!
Great work, Greg -- and you are as tenacious as I am :-)