SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a simple date functionality

    i am implementing a system where user should enter valid date but that field is not required..

    i mean i have one form and haveone date field, that is not required to be filled up, if filled up it must validate against a avalid date, i tried searching internet a lot but could not find the one..

    if regex can help, that will be a much lot help from u guys, Thanks and cheers

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the static Date.parse method.
    If the string supplied does not convert to a valid date it returns NaN, which == false.
    var str='january 25,2008';
    var str2='1/25/2008';
    alert(!!Date.parse(str))
    alert(!!Date.parse(str2));

    The nice thing about this method is that it is language specific, and uses the same date syntax as the local user.
    If you need the date, and the input didn't pass the parse, pop up a datepicker to get a known quantity.
    Fill the input with the toLocaleDateString of the selected date object.
    Last edited by mrhoo; Jan 25, 2008 at 14:09.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd look into a calendar picker widget that enforces a date format and do a regexp based on whatever format it uses to make sure it hasn't changed after the user picked it. People can enter dates in many ways and off the top of my head I don't know if Date.parse would work on:

    1-25-2008
    1.25.2008
    I'm assuming it works on 2-digit years.

    And what about European dates (which may not be applicable for you), where it's month, day, year? 25/1/2008

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by magenta placenta View Post
    People can enter dates in many ways and off the top of my head I don't know if Date.parse would work on:

    1-25-2008
    1.25.2008
    I'm assuming it works on 2-digit years.

    And what about European dates (which may not be applicable for you), where it's month, day, year? 25/1/2008
    It's already been said.

    Quote Originally Posted by mrhoo View Post
    The nice thing about this method is that it is language specific, and uses the same date syntax as the local user.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    will it check for a specific static date like: jan 1 2008 or it will just check the syntax of the value entered in the field as date.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Let's just say that Feb 29 2009 will not be a valid date.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •