javascript datetime validation??

Last post 12-05-2007 5:31 AM by venkatzeus. 4 replies.

Sort Posts:

  • javascript datetime validation??

    12-03-2007, 12:26 AM
    • Participant
      1,333 point Participant
    • venkatzeus
    • Member since 10-27-2006, 10:27 AM
    • Posts 535

    Hi..

    I have a javascript datetime picker calendar and a textbox in my web application. The user selects the datetime picker and uses the tab continuously. so at one point of time, there is no focus on the calender pick. Then the user clicks the enter button. The data which appears in the textbox is like this at this point of time: /11/2007 11:12:59

    As you can see, the date is not present(only the month,year and the time is present).. I want to validate this.

    How to check if the date time entered in the textbox is in proper format through javascript?

    Please help

  • Re: javascript datetime validation??

    12-03-2007, 2:17 AM
    Answer
    • Member
      612 point Member
    • Nail
    • Member since 08-18-2006, 2:22 PM
    • Russia
    • Posts 91

    javascript:

    var ctrl = document.getElementById('<your_textbox_clientID>');

    var v = ctrl.value;

    if (v.replace(/^\\s+|\\s+$/, '').length != 0 && isNaN(Date.parse(v))) { 
          alert('Invalid value!'); 
          ctrl.focus();
          return false;
    }

    This javascript allows value to be empty but if value is not empty then it has to be a valid datetime string. Yor input string has no day, so you need some formatting before checking:

    v = v.substr(1,2)+'/01/'+v.substr(4);

    For your example it will return "11/01/2007 11:12:59" (here 01 as "dummy" day).

    It is very unusually to have time part without day in the date part...

    [MCPD: WEB]
  • Re: javascript datetime validation??

    12-03-2007, 4:51 AM
    • Participant
      1,333 point Participant
    • venkatzeus
    • Member since 10-27-2006, 10:27 AM
    • Posts 535

    HI..

    Thank you very much for the reply.

    Does isNaN(Date.parse(v)) check for the date and time?

    There are situations when the calendar is only used for date. so I get only:  /01/2007

    Is this validation proper?

    var ctrl = document.getElementById('<your_textbox_clientID>');

    var v = ctrl.value;

    if (v.replace(/^\\s+|\\s+$/, '').length != 0 )

     { 
          alert('Invalid value!'); 
          ctrl.focus();
          return false;
    }

    Thank you

  • Re: javascript datetime validation??

    12-03-2007, 9:56 AM
    • Member
      612 point Member
    • Nail
    • Member since 08-18-2006, 2:22 PM
    • Russia
    • Posts 91

    Please, read this http://msdn2.microsoft.com/en-us/library/k4w173wk.aspx

    if (v.replace(/^\\s+|\\s+$/, '').length != 0 )  - it checks that entered value is not empty. it is incorrect in your case.

    You should use

    if (v.replace(/^\\s+|\\s+$/, '').length != 0 && isNaN(Date.parse(v))) 
    {
    // incorrect value;
    }

    Date.parse will check time part if present (but "Hours, minutes, and seconds are separated by colons, although all need not be specified. "10:", "10:11", and "10:11:12" are all valid").

    [MCPD: WEB]
  • Re: javascript datetime validation??

    12-05-2007, 5:31 AM
    • Participant
      1,333 point Participant
    • venkatzeus
    • Member since 10-27-2006, 10:27 AM
    • Posts 535

    Hi..

    Thank you very much for the reply.. Your reply really helped..

    Thank you 

     

Page 1 of 1 (5 items)