Problem with Regular Expression for Date

Asked By 0 points N/A Posted on -
qa-featured

Hi,

I use a regular expression to validate the date.

But it does not work properly. See my code :

function checkdateformat(userinput){
var dateformat = /^d{1,2}(-|/|.)d{1,2}1d4}$/
return dateformat.test(userinput) //returns true or false depending on userinput
}

Is there any other regular expression available to check the date format.

Thanks.

SHARE
Best Answer by Sharath Reddy
Answered By 0 points N/A #118714

Problem with Regular Expression for Date

qa-featured

Hi Crispin,

Yeah, I have mentioned an example for another regular expression to validate the date.

(d{1,2}(-|/|.)d{1,2}(-|/|.)d{4})

or

/^d{1,2}(-|/|.)d{1,2}(-|/|.)d{4}$/

If you are just going to use only "/" as the separator then you may simplify this as (d{1,2}/d{1,2}/d{4}).

Best Answer
Best Answer
Answered By 590495 points N/A #118715

Problem with Regular Expression for Date

qa-featured

Since you are doing a date validation on your website, here is one way of validating the date. The script displayed below is from the article created by Ed Nutting which you can also access by visiting.

Simply visit the site to learn about the complete explanation on the script’s function and how it works. You may try the script below by simply copying and pasting it on notepad, saving it on a file, and then upload it on your website.

//Value parameter – required. All other parameters are optional.

function isDate(value, sepVal, dayIdx, monthIdx, yearIdx) {
    try {
        //Change the below values to determine which format of date you wish to check. It is set to dd/mm/yyyy by default.
        var DayIndex = dayIdx !== undefined ? dayIdx : 0;
        var MonthIndex = monthIdx !== undefined ? monthIdx : 0;
        var YearIndex = yearIdx !== undefined ? yearIdx : 0;

        value = value.replace(/-/g, "/").replace(/./g, "/");
        var SplitValue = value.split(sepVal || "/");
        var OK = true;
        if (!(SplitValue[DayIndex].length == 1 || SplitValue[DayIndex].length == 2)) {
            OK = false;
        }
        if (OK && !(SplitValue[MonthIndex].length == 1 || SplitValue[MonthIndex].length == 2)) {
            OK = false;
        }
        if (OK && SplitValue[YearIndex].length != 4) {
            OK = false;
        }
        if (OK) {
            var Day = parseInt(SplitValue[DayIndex], 10);
            var Month = parseInt(SplitValue[MonthIndex], 10);
            var Year = parseInt(SplitValue[YearIndex], 10);

            if (OK = ((Year > 1900) && (Year < new Date().getFullYear()))) {
                if (OK = (Month <= 12 && Month > 0)) {

                    var LeapYear = (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0));

                    if(OK = Day > 0)
                    {
                        if (Month == 2) {
                            OK = LeapYear ? Day <= 29 : Day <= 28;
                        }
                        else {
                            if ((Month == 4) || (Month == 6) || (Month == 9) || (Month == 11)) {
                                OK = Day <= 30;
                            }
                            else {
                                OK = Day <= 31;
                            }
                        }
                    }
                }
            }
        }
        return OK;
    }
    catch (e) {
        return false;
    }
}

Related Questions