Validation the date

Hey, I’m still a bit stuck with the JS function that takes a string as an argument and returns a boolean indicating if the parameter string passed. There are some conditions, like:

A valid date string will have only the full month name(ex “January” not “Jan”), and a day. The string parameter can contain any number of spaces, but the month name must always start at the first non-space character from the beginning of the string.

So far it isn’t working properly…

     function validateTheDate(string){

const months = {
    January: {
        end: 31
    },
    February: {
        end: 28
    },
    March: {
        end: 30
    },
    April: {
        end: 31
    },
    May: {
        end: 30
    },
    June: {
        end: 31
    },
    July: {
        end: 30
    },
    August: {
        end: 31
    },
    September: {
        end: 30
    },
    October: {
        end: 31
    },
    November: {
        end: 30
    },
    December: {
        end: 31
    }

}

let i = 0;
let extractedMonth = ‘’;
let extractedDay = ‘’;

while (string[i] === ’ ’ && i < string.length) i++;

while (string[i] !== ’ ’ && i < string.length) extractedMonth += string[i++];

while (string[i] === ’ ’ && i < string.length) i++;

while (string[i] !== ’ ’ && i < string.length) extractedDay += string[i++];

if (months[extractedMonth]) {

}else {
return false;
}
return true;
}

Hi,
I would split the string parameter at the space character, then trim the extra spaces if still have some…
Remaining the name of month and a number.
At this point only have to check if the name is in the list of month and the day is bigger than zero and less than max value…

Thank you, very much, I will try that!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.