Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // initialize moment.js
- moment().format();
- $(document).ready(function() {
- for (m = 0; m <= 11; m++) {
- $('#month').append('<option value="' + m + '">' + moment().month(m).format('MMMM') + '</option>' );
- }
- $('.results-wrapper').hide();
- });
- // call validation when the submit button is clicked
- $('#preg-submit').on('click keyup', function(e) {
- return validateInput(e);
- });
- // also call validation when the enter key is clicked on any form element
- $('#month, #date, #year, #cycle-days').on('keyup', function(e) {
- // set a variable that handles the key event for all browsers
- var keycode = (event.keyCode ? event.keyCode : event.which);
- // if the return key (ASCII number 13) is pressed, run the function.
- if (keycode == '13') {
- return validateInput(e);
- }
- });
- // check the validity of the #cycle-days value before
- // we allow the data to be parsed and displayed
- function validateInput(e) {
- // Get input values
- var month = parseInt($('#month').val());
- var date = parseInt($('#date').val());
- var year = parseInt($('#year').val());
- var cycle = parseInt($('#cycle-days').val());
- return checkMonthInput();
- // check that a month has been selected
- function checkMonthInput() {
- if (!isNaN(month)) {
- // make sure the error message container is hidden
- $('.month-selector small').addClass('hidden');
- // check the year
- return checkYearInput();
- } else {
- // put the brakes on form submission
- e.stopPropagation();
- // rehide the content if it's been unhidden
- $('.results-wrapper').hide();
- // alert the user to the problem
- $('.month-selector small').addClass('has-error').removeClass('hidden').html('Please select a month.');
- // put focus back on the field that is missing information
- $('#month').focus();
- }
- }
- function checkYearInput() {
- var yearLength = $('#year').val().length;
- if (!isNaN(year) && yearLength === 4) {
- // make sure the error message container is hidden
- $('.year-input small').addClass('hidden');
- // check the date
- return checkDateInput();
- } else {
- // put the brakes on form submission
- e.stopPropagation();
- // rehide the content if it's been unhidden
- $('.results-wrapper').hide();
- // alert the user to the problem
- $('.year-input small').addClass('has-error').removeClass('hidden').text('Please enter the year in YYYY format. For example, this year is ' + moment().year());
- // put focus back on the field that is missing information
- $('#year').focus();
- }
- }
- // check that the date is valid given the month and year
- function checkDateInput() {
- var dateLength = $('#date').val().length;
- if (!isNaN(date) && (dateLength == 1 || dateLength == 2)) {
- console.log(moment(month).format('MMMM'));
- if (month === 1) {
- if (moment(year).isLeapYear() === false) {
- if (date > 28) {
- // put the brakes on form submission
- e.stopPropagation();
- // rehide the content if it's been unhidden
- $('.results-wrapper').hide();
- // alert the user to the problem
- $('.date-input small').addClass('has-error').removeClass('hidden').text('There are 28 days in ' + $('#month > option:selected').text() + ' in ' + moment().year() + '. Please correct your input.');
- // put focus back on the field that is missing information
- $('#date').focus();
- } else {
- // make sure the error message container is hidden
- $('.date-input small').addClass('hidden').removeClass('has-error');
- // check the cycle length
- return checkCycleLength();
- }
- } else if (moment(year).isLeapYear() === true) {
- if (date > 29) {
- // put the brakes on form submission
- e.stopPropagation();
- // rehide the content if it's been unhidden
- $('.results-wrapper').hide();
- // alert the user to the problem
- $('.date-input small').addClass('has-error').removeClass('hidden').text('There are 29 days in February in ' + $('#month > option:selected').text() + ' in ' + moment().year() + '. Please correct your input.');
- // put focus back on the field that is missing information
- $('#date').focus();
- } else {
- console.log(moment(month).format('MMMM'));
- // make sure the error message container is hidden
- $('.date-input small').addClass('hidden').removeClass('has-error');
- // check the cycle length
- return checkCycleLength();
- }
- }
- } else if (month === 3 || month === 5 || month === 8 || month === 10) {
- if (date > 30) {
- // put the brakes on form submission
- e.stopPropagation();
- // rehide the content if it's been unhidden
- $('.results-wrapper').hide();
- // alert the user to the problem
- $('.date-input small').addClass('has-error').removeClass('hidden').text('There are 30 days in ' + $('#month > option:selected').text() + '. Please correct your input.');
- // put focus back on the field that is missing information
- $('#date').focus();
- } else {
- // make sure the error message container is hidden
- $('.date-input small').addClass('hidden').removeClass('has-error');
- // check the cycle length
- return checkCycleLength();
- }
- } else {
- if (date > 31) {
- // put the brakes on form submission
- e.stopPropagation();
- // rehide the content if it's been unhidden
- $('.results-wrapper').hide();
- // alert the user to the problem
- $('.date-input small').addClass('has-error').removeClass('hidden').text('There are 31 days in ' + $('#month > option:selected').text() + '. Please correct your input.');
- // put focus back on the field that is missing information
- $('#date').focus();
- } else {
- // make sure the error message container is hidden
- $('.date-input small').addClass('hidden').removeClass('has-error');
- // check the cycle length
- return checkCycleLength();
- }
- }
- } else {
- // put the brakes on form submission
- e.stopPropagation();
- // rehide the content if it's been unhidden
- $('.results-wrapper').hide();
- // alert the user to the problem
- $('.date-input small').addClass('has-error').removeClass('hidden').text('Please correct your date entry. For example, today\'s date is: ' + moment().date());
- // put focus back on the field that is missing information
- $('#date').focus();
- }
- }
- // check to see if the cycle length has been entered
- function checkCycleLength() {
- if (cycle > 23 && cycle < 44) {
- // make sure the error message container is hidden
- $('.cycle-input small').removeClass('has-error has-warning').addClass('hidden');
- // check the cycle length
- return outputData();
- } else if (cycle <= 22 || cycle >= 45) {
- // if the cycle interval is too short
- if (cycle <= 22) {
- // alert the user and allow the calculation to continue
- $('.cycle-input small').addClass('has-warning').removeClass('hidden has-error').html('The cycle length you entered is too short to make an accurate prediction, but we\'ll do our best.');
- // process the provided data
- return outputData();
- } else if (cycle >= 45) {
- // alert the user and allow the calculation to continue
- $('.cycle-input small').addClass('has-warning').removeClass('hidden has-error').html('The cycle length you entered is too long to make an accurate prediction, but we\'ll do our best.');
- // process the provided data
- return outputData();
- }
- } else {
- // put the brakes on form submission
- e.stopPropagation();
- // rehide the content if it's been unhidden
- $('.results-wrapper').hide();
- // alert the user to the problem
- $('.cycle-input small').addClass('has-error').removeClass('hidden has-warning').html('We can\'t make a prediction until you\'ve completed all the fields. Please enter your cycle length.');
- // put focus back on the field that is missing information
- $('#cycle-days').focus();
- }
- }
- // give the user some data
- function outputData() {
- // set the date of the end of the user's last menstrual cycle
- var menstrualDate = moment().set({
- 'date': date,
- 'month': month,
- 'year': year
- });
- // TODO: Confirm that we only want to show the beginning of the conception period
- // set the ovulation date, conceptionStart date, and conceptionEnd date
- // so we can work with them and output data later.
- var ovulation = moment(menstrualDate + 86400000 * cycle - 1123200000);
- var conceptionStart = moment(ovulation - 432000000);
- //var conceptionEnd = moment(ovulation + 432000000);
- // Set the likely beginning date of the range
- // in which conception could occur
- $('.conception-begin').text(moment(conceptionStart).format('dddd, MMMM Do, YYYY'));
- // Set the likely ending date of the range
- // in which conception could occur
- //$('.conception-end').text(moment(conceptionEnd).format('dddd, MMMM Do, YYYY'));
- // Set the date of the end as the first trimester.
- // Per my source, the first trimester ends at
- // 83 days after the identified date of conception
- $('.first-trimester').text(moment(conceptionStart).add(83, 'days').format('dddd, MMMM Do, YYYY'));
- // Set the date of the end as the first trimester.
- // Per my source, the first trimester ends at
- // 83 days after the identified date of conception
- $('.second-trimester').text(moment(conceptionStart).add(195, 'days').format('dddd, MMMM Do, YYYY'));
- // Set the likely date of birth.
- // Per the internet (and if it's on the internet,
- // it must be true), the human gestational period
- // averages 280 days.
- $('.due-date').text(moment(conceptionStart).add(280, 'days').format('dddd, MMMM Do, YYYY'));
- //unhide the container
- $('.results-wrapper').fadeIn(600);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement