1

How can i validate a date input by the user to make sure that the date entered is within the accepted limit (e.g. date does not exceed 29 for feb, 30 for apr, jun, and nov and 31 for jan, mar, may, jul, aug, oct and dec) and the months not exceeding 12?

I have tried to add in conditions and the && simply does not work for me as it always returns an error.

My javascript function is as below:

function parseDate(s)
{
var dP = s.split("/");

var date = new Date(dP[2], (dP[1] - 1), dP[0]);

var dateStrParts = date.toString().split(" ");

return (date.getDate() + ", " + dateStrParts[1] + " " + dateStrParts[3]);
}

I have an onchange currently that will convert the month number to month name from user input and i believe i need to add another function for validation.

Hence how could i write the validation function and how should i add it into my onchange event?

There is no submit button for the date.

Thanks!

DKNguyen
  • 11
  • 2
  • 1
    How about using `Date.parse()` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse – source.rar Jun 09 '14 at 07:39
  • 2
    If it's in the format `d, MMM yyyy`, why are you splitting it on `/`, there is no `/` in that date format ? – adeneo Jun 09 '14 at 07:41
  • Hi @adeneo, could you suggest something for me to replace that with? i got that code from another user earlier and it worked fine hence i did not think of altering anything about it :) – DKNguyen Jun 09 '14 at 07:51
  • I have no idea, if it works it works, it just seemed strange that you say you have a date in that format, and then you split on /, when the format contains no such character ? – adeneo Jun 09 '14 at 07:52
  • well, it doesn't affect the way my code works and i guess it's fine to leave it the way it is! :) @adeneo – DKNguyen Jun 09 '14 at 07:59
  • http://stackoverflow.com/questions/14693298/js-check-for-valid-date-format/28777878#28777878 – Sony Mathew Mar 06 '15 at 07:18

1 Answers1

0

Use isNaN to check Date.parse()

function parseMyDate(s){

    if (isNaN(Date.parse(s))){
        alert("Your input has no logic at all");
        return;
    }

    var dateParts = s.split("/");

    var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

    var dateStrParts = date.toString().split(" ");

    return (date.getDate() + ", " + dateStrParts[1] + " " + dateStrParts[3]);
}

console.log(parseMyDate("31/02/2014"));

EDIT JSFiddle Demo

jhyap
  • 3,779
  • 6
  • 27
  • 47
  • Hi @jhyap, unfortunately if i entered an invalid date, it'll simply add onto the current day's date and still produce a date, therefore the result will never be 'NaN' so i believe i will have to set a condition for it. However i have no idea why the system does not accept the && operators. Do you have any idea how else a condition can be set? thank you so much you have helped me a lot! you helped me in my other acc but i couldn't log into that yesterday there was some problem with my email. – DKNguyen Jun 10 '14 at 01:42
  • http://stackoverflow.com/questions/24112958/how-to-format-datestring-to-d-mmm-yyyy-in-javascript – DKNguyen Jun 10 '14 at 02:31
  • the second part of that qn you helped me yesterday @jhyap – DKNguyen Jun 10 '14 at 02:31
  • I could not see a clue of your problem. I would guess you want to mean is when user enter `31/02/2014` it will prompt error message right? – jhyap Jun 10 '14 at 04:09
  • yeah but in the textbox and not an alert @jhyap – DKNguyen Jun 10 '14 at 04:28
  • You mean, when user key in `31/02/2014` inside a `textbox`. There is another message print somewhere in the browser by saying "you enter a wrong date" ? – jhyap Jun 10 '14 at 04:35
  • uhm when the user keys in '31/02/2014' the onchange event will activate and return 'Invalid Date' – DKNguyen Jun 10 '14 at 05:41
  • `onchange="$(this).val(parseMyDate($(this).val()))"` include my above example with the `Date.parse()`, you mean it wont give you a `NaN` even you the `$(this).val()` is invalid date? You are using a calendar and I doubt you can select `31/2/2014` in the calendar. Your user input is a selection from calendar or user typing input? – jhyap Jun 10 '14 at 06:01
  • The user input is both selection from calendar and user typing input. Yeap i included that and it doesn't give me a **Nan** on user typing input. I have no problems with user input selection from calendar. @jhyap – DKNguyen Jun 10 '14 at 06:22
  • if i entered, say, `36/06/2014` the results will be `6 Jul, 2014`. @jhyap – DKNguyen Jun 10 '14 at 06:25
  • I updated the question with a jsfiddle, it is catching `NaN` with no problem. – jhyap Jun 10 '14 at 06:39
  • that's weird. do you think it's because of my calendar? because if i entered a date that's beyond the limits of that month, it will simply add it together. For example `36/06/2014` would add up with today's date, `10/06/2014` and produce `06/07/2014`. Is there anyway that i could show you...? perhaps screenshot? @jhyap – DKNguyen Jun 10 '14 at 06:52
  • If i entered `31/02/2014` it'll return `3 Mar, 2014` @jhyap – DKNguyen Jun 10 '14 at 06:56
  • Yes. You can add screenshoot with reputation more than 10. – jhyap Jun 10 '14 at 07:03
  • would if/else statements be possible? @jhyap – DKNguyen Jun 10 '14 at 07:17
  • the problem is not about if-else, is about it auto correct your input with increment of next following days. Check with your supervisor how does it work and prevent auto-correction if necessary. – jhyap Jun 10 '14 at 07:24