Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 744
  • Last Modified:

Using jquery validation, how do I validate a Date of Birth with 3 different select boxes?

I have a signup form that I am validating using jquery, and I am trying to validate a Date of Birth that is spread across 3 different select boxes.

You can see this form at
http://www.rewardsring.com/sandbox/signup2V.html

My current approach is to create a hidden field next to the three select boxes and validate that instead (with the associated code necessary to build the value of that field).
I then wrote my own validation method (properDate) to make sure that dates like September 31st aren't permitted.
I also want the three select boxes to appear red if there is a problem with the date.

Unforunately, it doesn't seem to work at all, and I have no idea how to fix it, so any help is appreciated. (For example, Feb-31-1984 seems to be a valid date!?!?!)
0
sjamal_hsj
Asked:
sjamal_hsj
  • 3
  • 2
1 Solution
 
dxdinhCommented:
1.  you can add the check valid date function and test against
   

    var dob1 =   $("#user_dob").val();
  if (!checkDate(dob1)) {
        // focus if validation fails
       alert ('Please correct dob');
    }


2.  or you can pre populate based on user selection cascading
   Feb then populate 28 day
   if select leap year then populate 29 days....  of course you have to change a lot in your logic.



<!--
     Example File From "JavaScript and DHTML Cookbook"
     Published by O'Reilly & Associates
     Copyright 2003 Danny Goodman
-->

function checkDate(fld) {
    var mo, day, yr;
    var entry = fld.value;
    var re = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    if (re.test(entry)) {
        var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        mo = parseInt(entry.substring(0, delim1), 10);
        day = parseInt(entry.substring(delim1+1, delim2), 10);
        yr = parseInt(entry.substring(delim2+1), 10);
        var testDate = new Date(yr, mo-1, day);
        alert(testDate)
        if (testDate.getDate() == day) {
            if (testDate.getMonth() + 1 == mo) {
                if (testDate.getFullYear() == yr) {
                    return true;
                } else {
                    alert("There is a problem with the year entry.");
                }
            } else {
                alert("There is a problem with the month entry.");
            }
        } else {
            alert("There is a problem with the date entry.");
        }
    } else {
        alert("Incorrect date format. Enter as mm/dd/yyyy.");
    }
    return false;
}
0
 
sjamal_hsjAuthor Commented:
In regards to the first solution, that doesn't really use jquery. Technically, using javascript, I know how to test for a valid date, but I wanted to do it using the jquery validation plugin.

For the second solution, it would work, but it does require a large amount of extra code. Also, it wouldn't solve my problem of validating 3 select boxes as a group using jquery.

Thanks for the input though
0
 
dxdinhCommented:
I guess the question s/b how to extend jQuery validation plugin to date selection drop down list box.
0
 
sjamal_hsjAuthor Commented:
To be honest, I'm not sure if it needs to be extended. I'm new to jquery, and I was wondering if it was possible, using the jquery validation plugin, to validate a date that uses 3 select boxes.

If not, then I suppose I can change it to an input box.
0
 
sjamal_hsjAuthor Commented:
my question was perhaps more vague than it needed to be
0
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now