SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: date validation

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    India
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    date validation

    I have a text field like that:


    <input type=text name=date >

    In this text field the user can enter the date in dd/mm/yyyy format.

    but the future date is not allowed, except the past and present date is allowed.


    thanx


    abhijit

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1)Check to see if it is a valid date according to your format. Search the web for the hundreds of regular expressions for validating a date. You can start here: http://www.regexlib.com/DisplayPatte...5&categoryId=5

    2) If it is a valid date according to your format, use split('/') to split the string into days, months and years.

    var date_array=dateEntered.split('/');

    3) Create a date object for the entered date:

    var enteredD = new Date(date_array[2], date_array[1], date_array[0]).

    4) Create a date object for the current date:

    var currentD = new Date();

    5) Subtract the date objects:

    var difference= currentD - enteredD;

    6) If the difference is positive, then the date is valid. If the difference is negative, the entered date is in the future.

    7) You have to figure out how to deal with dates before 1970 because as far as I know the Date object can only handle comparing dates after 1970.

    An alternate method would be to compare the entered year stored in date_array[2] to the current year. You can get the current year from currentD like this:

    var year = currentD.getFullYear();

    If the entered year is more than the current year then the date is invalid; if it is less it is valid. If the years are the same, then you have to check the months:

    var month=currentD.getMonth();

    Months are indexed from 0 to 11, so you need to add 1 to that result to compare it to the entered month. If the entered month is greater than the current month, then the date is invalid; if it is less, then the date is valid. If it is the same, you have to check the days.

    The days of the month are called the "date" in javascript:

    var days = currentD.getDate();

    The days are indexed normally from 1-31.

    I hope that helps.

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not use a calendar widget? They're - and many are free.

    http://www.dynarch.com/projects/calendar/
    ::: certified wild guess :::

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by adios
    Why not use a calendar widget? They're - and many are free.

    http://www.dynarch.com/projects/calendar/
    ...and users won't screw up date formatting

Bookmarks

Posting Permissions

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