0

In my form, there are two label fields namely, date of birth and confirm date of birth. Each field consists of three input fields for DD, MM and YY respectively. Below is the html code,

     <label>Date of Birth</label>
     <input type="textbox" id="dd1" />
     <input type="textbox" id="mm1" /> 
     <input type="textbox" id="yy1" />
     <label>Confirm Date of Birth</label>
     <input type="textbox" id="dd2" />
     <input type="textbox" id="mm2" /> 
     <input type="textbox" id="yy2" />

Now i need to compare both label fields. If both are equal, need to get an alert message. Please help...

steeve
  • 699
  • 3
  • 10
  • 19
  • 1
    What exactly do you need help with? Getting a reference to the DOM elements? Getting their value? Comparing the values? Showing the alert? Or executing the JavaScript at the right time? What have you tried so far? – Felix Kling Jul 26 '12 at 09:14

4 Answers4

0

Just create a function that accepts the number (1 or 2) and then insid with jQuery do a concat of the $("#XX" +param).value (where XX is dd, mm , yy and param is the received parameter).

Create a date from the string and return it.

I would suggest you the use of HTML5 look at html5-date

Rui Lima
  • 7,185
  • 4
  • 31
  • 42
0

For Comparing two javascript dates this link might be useful for you

Compare two dates with JavaScript

Community
  • 1
  • 1
Ravi
  • 3,132
  • 5
  • 24
  • 35
0

You can just parse them to Date objects:

var y1 = document.getElementById('yy1').value;
// get the rest input value
var date = new Date(y1, m1, d1)
  , confirmed_date = new Date(y2, m2, d2);
if (date == confirmed_date) { /* do something */ }

The above code also can validate if user's input is a valid date or not.

More info about Date object: http://www.javascriptkit.com/jsref/date.shtml

Chris
  • 1,264
  • 1
  • 10
  • 14
0
var d1 = document.getElementById('dd1').value;
var m1 = document.getElementById('mm1').value;
var y1 = document.getElementById('yy1').value;

var d2 = document.getElementById('dd2').value;
var m2 = document.getElementById('mm2').value;
var y2 = document.getElementById('yy2').value;

var date1 = new Date(y1 + "/" + m1 + "/" + d1);
var date2 = new Date(y2 + "/" + m2 + "/" + d2);

date1 = date1.getTime();
date2 = date2.getTime();

if (date1 == date2) {
    alert('Dates are equal');
} else {
    alert('Dates are not equal');
}

This works as pure javascript, OR you can use jquery replacing

document.getElementByIt('dd1').value;

with $('#dd1').val();

OR you can use a datepicker, i would suggest jquery-ui http://jqueryui.com/demos/datepicker/

and compare the dates from the 2 fields populated by the datepicker.

CKKiller
  • 1,424
  • 1
  • 16
  • 20