Validate date using jquery

impressionexpress
impressionexpress used Ask the Experts™
on
I have a date picker that enters the selected date inside the textbox. I need to make sure of the following before I save the date:

Make sure the field is not empty
Make sure the date is in YYYY-MM-DD format (ready to store in mysql date type column)
Make sure the date is at least 18yrs ago

				//Last Name
				var input= $("#last_name");
				var last_name = input.val();
				if(last_name != "" && validate_letters_only(last_name)){
					fd.append('last_name', last_name);
					input.removeClass("invalid").addClass("valid");
				}else{
					input.removeClass("valid").addClass("invalid");
				}
				
				// Birthday
				fd.append('birthdate', $("#birthday").val());

Open in new window


Thanks in advance for your help
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
//Last Name
var input= $("#last_name");
var last_name = input.val();
if(last_name != "" && validate_letters_only(last_name)){
    fd.append('last_name', last_name);
    input.removeClass("invalid").addClass("valid");
}else{
    input.removeClass("valid").addClass("invalid");
}

// Birthday
var birthday = $("#birthday").val();
if(birthday.length==10 && /\d\d\d\d-\d\d-\d\d/.test(birthday)) {
    var d = new Date(birthday.substring(0,4), birthday.substring(5,7)*1-1, birthday.substring(8,10) );
    if(d!=null && d.getDate() == birthday.substring(8,10)*1 && d.getMonth()*1+1 == birthday.substring(5,7)*1 && d.getFullYear() == birthday.substring(0,4) ) {
        fd.append('birthdate', birthday);
        $("#birthday").removeClass("invalid").addClass("valid");
    } else {
        $("#birthday").removeClass("valid").addClass("invalid")
    }
} else {
    $("#birthday").removeClass("valid").addClass("invalid")
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial