Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Noblesville, IN
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validating that start date is earlier than the end date

    I have 2 fields, a start date and an end date.

    how can i validate it so that the end date is later than the start date?

    right now i am validating that the dates are entered in the mm/dd/yyyy format.

    thanks,
    Ed Ventura
    Graphic/Web Designer
    www.blackwatercompany.com

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    can we see the code you're using right now? i know how to do it, but i'd like see what you're already doing, so that there won't be an compatability problems between what i say, and what you've got. it reduces the probability of me feeling like an idiot later. ::grin::
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    New Coder
    Join Date
    Jul 2002
    Location
    Noblesville, IN
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function validateGenReport() {
    var str = document.form;
    var dateFormat = /\d\d\/\d\d\/\d\d\d\d/; // mm/dd/yyyy

    if (str.datebox.value == '') {
    alert('Please enter a start date in mm/dd/yyyy format.');
    str.datebox.value = 'mm/dd/yyyy';
    str.datebox.focus();
    str.datebox.select();
    return false;
    }
    else if (str.datebox.value != '') {
    if (!(str.datebox.value.match(dateFormat))) {
    alert('The start date:\n \n ' + str.datebox.value + ' is not in the correct format.\n \nThe date must only contain numbers and be in the following format:\n \n mm/dd/yyyy');
    str.datebox.value = 'mm/dd/yyyy';
    str.datebox.focus();
    str.datebox.select();
    return false;
    }
    }
    if (str.datebox2.value != '') {
    if (!(str.datebox2.value.match(dateFormat))) {
    alert('The end date:\n \n ' + str.datebox2.value + ' is not in the correct format.\n \nThe date must only contain numbers and be in the following format:\n \n mm/dd/yyyy');
    str.datebox2.value = 'mm/dd/yyyy';
    str.datebox2.focus();
    str.datebox2.select();
    return false;
    }
    }
    MM_showHideLayers('Layer1','','show');
    //alert('Processing a report may take up to 30 seconds to complete.\n \nPlease click "OK" to start processing your report.');
    }
    Ed Ventura
    Graphic/Web Designer
    www.blackwatercompany.com

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function dateAtts(mm,dd,yyyy) {
    var d=new Date();
    d.setMonth(mm+1);
    d.setDate(dd);
    d.setFullYear(yyyy);
    return d;
    }
    function validDate(start,end) {
    var re=/\d{2}\/\d{2}\/\d{4}/;
    if(!re.test(start) || !re.test(end)) return false;
    
    var newStart=start.split("/");
    var newEnd=end.split("/");
    
    var sDate=dateAtts(newStart[0],newStart[1],newStart[2]);
    var eDate=dateAtts(newEnd[0],newEnd[1],newEnd[2]);
    return (sDate.parse()<eDate.parse());
    }
    Hope that helps!

    Happy coding!

    Edit: Don't mind the line feed in the declaration of the sDate variable. For some reason it won't remove it although it shouldn't be there.
    Last edited by nolachrymose; 08-08-2002 at 05:53 PM.

  • #5
    New Coder
    Join Date
    Jul 2002
    Location
    Noblesville, IN
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks nolachrymose, but i'm not sure where i should put this code in my existing code...

    thanks,
    Ed Ventura
    Graphic/Web Designer
    www.blackwatercompany.com

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Enough of this nonsense Do y'all get paid by the line of code?

    Here's all you need:
    Code:
    var endDate  = new Date()                      // defaults to today's date
    var startDate = new Date (2002, 07, 21) // July 21, 2002
    
    if (endDate  > startDate) {
       // the end date is later
    }else{
       // the end date is at or before the start date
    }
    Yes! It's that simple. The miracle of object-base Javascript. The reason this simple arithmatic expression works is that dates are stored internally in milliseconds; so you can do arithmetic with dates!
    Last edited by RadarBob; 08-09-2002 at 03:03 AM.

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Ames, IA, USA
    Posts
    373
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My thoughts exactly, RadarBob. However, the date set in your example is actually Aug 21, 2002 rather than July 21, 2002. If ventura wants to set the date variable with values entered by users, I expect something like this might be in order:
    Code:
    var startDate = new Date (yyyy, mm-1, dd)
    And rather than messing with that complex verification scheme and the substring extraction routine, I'd go with three text input boxes -- should simplify the code significantly.
    Need more emoticons?
    Visit Catman's Private Stock

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by RadarBob
    Enough of this nonsense Do y'all get paid by the line of code?
    The additional lines in nolachrymose' code are for the validation of the correct format of the data input prior to constructing the date object's, I fail to see any nonsense there.

    Also, your suggestion

    if (endDate > startDate)

    is just some digits less than nolachrymose' example and does exactly the same:

    return (sDate.parse()<eDate.parse());

    Plus that you have a straight procedural code flow whereas the other example encapsulated everything in functions, which makes it better reusable.
    So where's the nonsense?

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    However, the date set in your example is actually Aug 21, 2002 rather than July 21, 2002.
    From The Core Javascript Guide on www.netscape.......
    With the "get" and "set" methods you can get and set seconds, minutes, hours, day of the month, day of the week, months, and years separately. There is a getDay method that returns the day of the week, but no corresponding setDay method, because the day of the week is set automatically. These methods use integers to represent these values as follows:

    Seconds and minutes: 0 to 59

    Hours: 0 to 23

    Day: 0 (Sunday) to 6 (Saturday)

    Date: 1 to 31 (day of the month)

    Months: 0 (January) to 11 (December)

    Year: years since 1900

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Location
    Ames, IA, USA
    Posts
    373
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, so 7 is August.
    Need more emoticons?
    Visit Catman's Private Stock


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •