1

I have a date picker which I am using to input date. I want to check that whether the the date we are getting as an input is correct or not. e.g. - 13/13/2013 or 96/25/1995 and many such dates have to be declared wrong before posting to DataBase. I submitted the project but testers want this feature to be also there.

Javascript -

if ($(event.target).is("#AddCustomerDeliveryDate"))
    {
        $("#AddCustomerDeliveryDate").datepicker({
            showOn: 'focus',
            minDate: null,
            dateFormat: 'dd/mm/yy'
        }).focus();
    }

HTML -

 <td><input type="text"  name="CustomerDeliveryDate" id="AddCustomerDeliveryDate" required></td>
qwerrt
  • 29
  • 3
  • What have you tried so far? – Adnan S Jul 09 '18 at 06:24
  • 1
    @AhmedS added my code above. – qwerrt Jul 09 '18 at 06:25
  • 2
    1. Make the field type="date" 2. Check on the server. I can EASILY change the html and post whatever I want to your DB regardless of validation on the client – mplungjan Jul 09 '18 at 06:30
  • 1
    Possible duplicate of [jquery datepicker: validate date](https://stackoverflow.com/questions/27528068/jquery-datepicker-validate-date) – Mr. Roshan Jul 09 '18 at 06:32
  • 1
    @mplungjan The problem with date type is that it sends the date format to db in always yyyy-mm-dd and you can't change it. – qwerrt Jul 09 '18 at 06:33
  • 1
    It does no such thing. It may send a date format you do not WANT in the DB, but then you change it to what you want BEFORE storing it. That is standard procedure. – mplungjan Jul 09 '18 at 06:34
  • 1
    @mplungjan How to change the format. Searched throughout the internet only to find it can't be changed. – qwerrt Jul 09 '18 at 06:36
  • 1
    Change it on the server OR Store the perfectly great format yyyy-mm-dd – mplungjan Jul 09 '18 at 06:41
  • 1
    @mplungjan Manager wants dd-mm-yyyy :( – qwerrt Jul 09 '18 at 06:44
  • Store it in `yyyy-mm-dd` format and display it in `dd-mm-yyyy` format? – Thum Choon Tat Jul 09 '18 at 07:13
  • Or convert to dd-mm-yyyy before storing it the way the manager wants – mplungjan Jul 09 '18 at 07:23
  • There are literally 100s of questions dealing with this: https://www.google.nl/search?q=jquery+datepicker+dd-mm-yyyy+format+site:stackoverflow.com AND this: https://www.google.nl/search?q=jquery+datepicker+validate+site%3Astackoverflow.com – mplungjan Jul 09 '18 at 07:25

1 Answers1

1

Moment JS is a famous library for doing a lot of things with dates. Please have a look here to validate with Moment.

If you don't like to load a library for this then it is possible to do with Raw JavaScript real quick. Have a look on this answer. I am copying it here for you. It will validate mm/dd//yyyy formatted date.

// Validates that the input string is a valid date formatted as "mm/dd/yyyy"
function isValidDate(dateString)
{
    // First check for the pattern
    if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
        return false;

    // Parse the date parts to integers
    var parts = dateString.split("/");
    var day = parseInt(parts[1], 10);
    var month = parseInt(parts[0], 10);
    var year = parseInt(parts[2], 10);

    // Check the ranges of month and year
    if(year < 1000 || year > 3000 || month == 0 || month > 12)
        return false;

    var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

    // Adjust for leap years
    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
        monthLength[1] = 29;

    // Check the range of the day
    return day > 0 && day <= monthLength[month - 1];
};

I think this code is exactly what you need to validate, it will validate dd/mm/yyyy formatted date as yours. I customized it a little bit for you.

function isValidDate(dateString){
    // First check for the pattern for dd/mm/yyyy
    var regex_date = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

    if(!regex_date.test(dateString)){
        return false;
    }

    // Parse the date parts to integers
    var parts   = dateString.split("/");
    var day     = parseInt(parts[0], 10);
    var month   = parseInt(parts[1], 10);
    var year    = parseInt(parts[2], 10);

    // Check the ranges of month and year
    if(year < 1000 || year > 3000 || month == 0 || month > 12){
        return false;
    }

    var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

    // Adjust for leap years
    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)){
        monthLength[1] = 29;
    }

    // Check the range of the day
    return day > 0 && day <= monthLength[month - 1];
}

Also Date JS is worth a try. you can follow this way

asifsaho
  • 507
  • 1
  • 4
  • 20