You must Sign In to post a response.
  • Category: JavaScript

    DateTime validation in Javascript

    Hi guys,

    i would like to validate the DateTime field in Javascript Please tell me the any suggestion...


    regards,
    Parshu
  • #266736
    hi

    yes we to validate the datetime functions in Javascript

    i have write the function for compare the dates

    /***********************************************
    Function : DateCompare
    Purpose : Validation For Date
    Inputs : (date1Str,date2Str)Date
    Output : (alert)String
    Using : RUD_RATE
    ************************************************/

    function DateCompare (date1Str, date2Str)
    {
    var date1 = new Date(date1Str.value);
    var date2 = new Date(date2Str.value);
    if (date1 > date2)
    {
    alert ("End Date is less then Start Date");
    return false;
    }
    else
    {
    return true;
    }
    }

  • #266741
    Ashok my question is how to Validate the DateTime Field for Example 05/05/2005 12:00:00 AM the entire field need to validate

  • #266749
    Hi,

    <script type="text/javascript" language="JavaScript">
    function check_date(field){
    var checkstr = "0123456789";
    var DateField = field;
    var Datevalue = "";
    var DateTemp = "";
    var seperator = ".";
    var day;
    var month;
    var year;
    var leap = 0;
    var err = 0;
    var i;
    err = 0;
    DateValue = DateField.value;
    /* Delete all chars except 0..9 */
    for (i = 0; i < DateValue.length; i++) {
    if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
    DateTemp = DateTemp + DateValue.substr(i,1);
    }
    }
    DateValue = DateTemp;
    /* Always change date to 8 digits - string*/
    /* if year is entered as 2-digit / always assume 20xx */
    if (DateValue.length == 6) {
    DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
    if (DateValue.length != {
    err = 19;}
    /* year is wrong if year = 0000 */
    year = DateValue.substr(4,4);
    if (year == 0) {
    err = 20;
    }
    /* Validation of month*/
    month = DateValue.substr(0,2);
    if ((month < 1) || (month > 12)) {
    err = 21;
    }
    /* Validation of day*/
    day = DateValue.substr(2,2);
    if (day < 1) {
    err = 22;
    }
    /* Validation leap-year / february / day */
    if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
    leap = 1;
    }
    if ((month == 2) && (leap == 1) && (day > 29)) {
    err = 23;
    }
    if ((month == 2) && (leap != 1) && (day > 2) {
    err = 24;
    }
    /* Validation of other months */
    if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
    err = 25;
    }
    if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
    err = 26;
    }
    /* if 00 ist entered, no error, deleting the entry */
    if ((day == 0) && (month == 0) && (year == 00)) {
    err = 0; day = ""; month = ""; year = ""; seperator = "";
    }
    /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
    if (err == 0) {
    DateField.value = day + seperator + month + seperator + year;
    }
    /* Error-message if err != 0 */
    else {
    alert("Date is incorrect!");
    DateField.select();
    DateField.focus();
    }
    }
    // End -->
    </script>

    All is well.
    Thanks,
    Dharma
    Editor,Mentor,MVM
    Try and fail but don't fail and try

  • #266754
    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..

  • #266755
    hi,


    <script type="text/javascript" language="JavaScript">
    <!-- The JavaScript Source!! http://javascript.internet.com -->
    <!-- Original: Torsten Frey () -->
    <!-- Begin
    function check_date(field){
    var checkstr = "0123456789";
    var DateField = field;
    var Datevalue = "";
    var DateTemp = "";
    var seperator = ".";
    var day;
    var month;
    var year;
    var leap = 0;
    var err = 0;
    var i;
    err = 0;
    DateValue = DateField.value;
    /* Delete all chars except 0..9 */
    for (i = 0; i < DateValue.length; i++) {
    if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
    DateTemp = DateTemp + DateValue.substr(i,1);
    }
    }
    DateValue = DateTemp;
    /* Always change date to 8 digits - string*/
    /* if year is entered as 2-digit / always assume 20xx */
    if (DateValue.length == 6) {
    DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
    if (DateValue.length != {
    err = 19;}
    /* year is wrong if year = 0000 */
    year = DateValue.substr(4,4);
    if (year == 0) {
    err = 20;
    }
    /* Validation of month*/
    month = DateValue.substr(0,2);
    if ((month < 1) || (month > 12)) {
    err = 21;
    }
    /* Validation of day*/
    day = DateValue.substr(2,2);
    if (day < 1) {
    err = 22;
    }
    /* Validation leap-year / february / day */
    if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
    leap = 1;
    }
    if ((month == 2) && (leap == 1) && (day > 29)) {
    err = 23;
    }
    if ((month == 2) && (leap != 1) && (day > 2) {
    err = 24;
    }
    /* Validation of other months */
    if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
    err = 25;
    }
    if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
    err = 26;
    }
    /* if 00 ist entered, no error, deleting the entry */
    if ((day == 0) && (month == 0) && (year == 00)) {
    err = 0; day = ""; month = ""; year = ""; seperator = "";
    }
    /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
    if (err == 0) {
    DateField.value = day + seperator + month + seperator + year;
    }
    /* Error-message if err != 0 */
    else {
    alert("Date is incorrect!");
    DateField.select();
    DateField.focus();
    }
    }
    // End -->
    </script>

  • #266835
    //Date Of Birth
    var dob=document.getElementById('txtDateofBirth');
    var doj=document.getElementById('txtDateofJoin');

    if(dob.value=="")
    {
    alert('Date of Birth is Required');
    return false;
    }
    if(!isValidDate(dob.value))
    {
    return false;
    }
    if(!checkBirthday())
    {
    return false;
    }
    function isValidDate(dateStr)
    {
    // Checks for the following valid date formats:
    // MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
    // Also separates date into month, day, and year variables

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

    // To require a 4 digit year entry, use this line instead:
    // var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

    var matchArray = dateStr.match(datePat); // is the format ok?
    if (matchArray == null)
    {
    alert("Date is not in a valid format.")
    return false;
    }
    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[4];
    if (month < 1 || month > 12)
    {
    // check month range
    alert("Month must be between 1 and 12");
    return false;
    }
    if (day < 1 || day > 31) {
    alert("Day must be between 1 and 31");
    return false;
    }
    if ((month==4 || month==6 || month==9 || month==11) && day==31)
    {
    alert("Month "+month+" doesn't have 31 days!")
    return false
    }
    if (month == 2)
    { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day>29 || (day==29 && !isleap)) {
    alert("February " + year + " doesn't have " + day + " days!");
    return false;
    }
    }
    return true; // date is valid
    }

    function checkBirthday()
    {
    var thetime=new Date();
    var nday=thetime.getDay();
    var nmonth=thetime.getMonth();
    var ntoday=thetime.getDate();
    var nyear=thetime.getYear();
    if (nyear<=99)
    {
    nyear= "19"+nyear;
    }

    if ((nyear>99) && (nyear<2000))
    nyear+=1900;
    if(ntoday<10)
    {
    ntoday="0"+ntoday;
    }
    if(nmonth<10)
    {
    nmonth="0"+nmonth;
    }

    var currentDate=nmonth+"/"+ntoday+"/"+nyear;
    var UserDate=document.getElementById('txtDateofBirth').value;
    if(Date.parse(UserDate)>=Date.parse(currentDate))
    {
    alert('Invalid Date of Birth');
    return false;
    }
    return true;

    }

  • #267711
    hi,

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


    try this code

  • #268078
    Thanks to All


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.