check Date format : Regular Expressions « Development « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Event
7. Event onMethod
8. Form Control
9. GUI Components
10. HTML
11. Javascript Collections
12. Javascript Objects
13. Language Basics
14. Node Operation
15. Object Oriented
16. Page Components
17. Security
18. Style Layout
19. Table
20. Utilities
21. Window Browser
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
JavaScript DHTML » Development » Regular Expressions 
check Date format

<!-- 
     Example File From "JavaScript and DHTML Cookbook"
     Published by O'Reilly & Associates
     Copyright 2003 Danny Goodman
-->

function checkDate(fld) {
    var mo, day, yr;
    var entry = fld.value;
    var re = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    if (re.test(entry)) {
        var delimChar = (entry.indexOf("/"!= -1"/" "-";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        mo = parseInt(entry.substring(0, delim1)10);
        day = parseInt(entry.substring(delim1+1, delim2)10);
        yr = parseInt(entry.substring(delim2+1)10);
        var testDate = new Date(yr, mo-1, day);
        alert(testDate)
        if (testDate.getDate() == day) {
            if (testDate.getMonth() == mo) {
                if (testDate.getFullYear() == yr) {
                    return true;
                else {
                    alert("There is a problem with the year entry.");
                }
            else {
                alert("There is a problem with the month entry.");
            }
        else {
            alert("There is a problem with the date entry.");
        }
    else {
        alert("Incorrect date format. Enter as mm/dd/yyyy.");
    }
    return false;
}

function validateDate(fld) {
    if (!checkDate(fld)) {
        // focus if validation fails
        fld.focus();
        fld.select();
    }
}

----------

function checkDate(fld) {
    var mo, day, yr;
    var entry = fld.value;
    var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
    var valid = (reLong.test(entry)) || (reShort.test(entry));
    if (valid) {
        var delimChar = (entry.indexOf("/"!= -1"/" "-";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        mo = parseInt(entry.substring(0, delim1)10);
        day = parseInt(entry.substring(delim1+1, delim2)10);
        yr = parseInt(entry.substring(delim2+1)10);
        // handle two-digit year
        if (yr < 100) {
            var today = new Date();
            // get current century floor (e.g., 2000)
            var currCent = parseInt(today.getFullYear() 100100;
            // two digits up to this year + 15 expands to current century
            var threshold = (today.getFullYear() 15- currCent;
            if (yr > threshold) {
                yr += currCent - 100;
            else {
                yr += currCent;
            }
        }
        var testDate = new Date(yr, mo-1, day);
        if (testDate.getDate() == day) {
            if (testDate.getMonth() == mo) {
                if (testDate.getFullYear() == yr) {
                    // fill field with database-friendly format
                    fld.value = mo + "/" + day + "/" + yr;
                    return true;
                else {
                    alert("There is a problem with the year entry.");
                }
            else {
                alert("There is a problem with the month entry.");
            }
        else {
            alert("There is a problem with the date entry.");
        }
    else {
        alert("Incorrect date format. Enter as mm/dd/yyyy.");
    }
    return false;
}


           
       
Related examples in the same category
1. Searching and Replacing Substrings
2. Regular Expression Tester
3. The Regular Expression Tester
4. Regular Expression Match Workshop
5. Regular Expressions: Looking for a Match
6. Regular Expressions: Extracting Data from a Match
7. Regular Expressions: Replacing Strings via Regular Expressions
8. Split comma number string
w_ww.___j___a_v__a2_s.c_o___m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.