Results 1 to 4 of 4

Thread: Regex Date Validation

  1. #1

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    Regex Date Validation

    Im using this string in javascript to validate a date field upon blur event. Im no regex expert but what am I missing that is keeping this from working? I need it to validate a mm/dd/yyyy, m/d/yyyy, mm/d/yyyy, m/dd/yyyy and also to include leap year.

    This is the string I have so far but it lets 13/10,2020 pass.

    Code:
    ((0?|0[13578]|1[02])[\/.](0[1-9]|[12][0-9]|3[01])[\/.](19|20)[0-9]{2})
    |
    ((0?|0[469]|11)[\/.](0[1-9]|[12][0-9]|30)[\/.](19|20)[0-9]{2})|((02)[\/.](0[1-9]|1[0-9]|2[0-8])[\/.](19|20)[0-9]{2})
    |
    ((2)|(02)[\/.]29[\/.](((19|20)(04|08|[2468][048]|[13579][26]))|2000))

    Think this should work for just the month validation (13578 months only to break this down simplier)?
    (([13578])|0[13578]|1[02])

    Thanks
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    Re: Regex Date Validation

    This works without leap year

    Code:
    ^((0?[13578]|10|12)(-|\/)(([1-9])|(0[1-9])|([12])([0-9]?)|(3[01]?))(-|\/)((19|20)([0-9])(\d{1})|(20)([01])(\d{1})|([8901])(\d{1}))
    |
    (0?[2469]|11)(-|\/)(([1-9])|(0[1-9])|([12])([0-9]?)|(3[0]?))(-|\/)((19|20)([0-9])(\d{1})|(20)([01])(\d{1})|([8901])(\d{1})))$
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Re: Regex Date Validation

    HTML

    Code:
    <form>
        <p>Enter date</p>
            <input type="text" id="test1" value="22.05.2013 11:23:22"/>
       
    </form>
    JS

    Code:
    $(document).ready(function(){
        $('#test1').blur(function(){
            if (is_valid_date($(this).val())) {
                $(this).css('background', '#dfd');
            } else {
                $(this).css('background', '#fdd');
            }
        });
    });
    
    function is_valid_date(value) {
        // capture all the parts
        var matches = value.match(/^(\d{2})\.(\d{2})\.(\d{4}) (\d{2}):(\d{2}):(\d{2})$/);
        if (matches === null) {
            return false;
        } else{
            // now lets check the date sanity
            var year = parseInt(matches[3], 10);
            var month = parseInt(matches[2], 10) - 1; // months are 0-11
            var day = parseInt(matches[1], 10);
            var hour = parseInt(matches[4], 10);
            var minute = parseInt(matches[5], 10);
            var second = parseInt(matches[6], 10);
            var date = new Date(year, month, day, hour, minute, second);
            if (date.getFullYear() !== year
              || date.getMonth() != month
              || date.getDate() !== day
              || date.getHours() !== hour
              || date.getMinutes() !== minute
              || date.getSeconds() !== second
            ) {
               return false;
            } else {
               return true;
            }
        
        }
    }
    Try like this!!!

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,614

    Re: Regex Date Validation

    I would not try to reinvent the wheel. MomentJS is a reliable library that allows you to parse, validate, manipulate, and display dates/times in JavaScript.

    In this case, it would be as simple as:
    Code:
    const inputBlur = function(e) {
        const value = e.target.value;
        const conversion = moment(value);
    
        console.log(`${value} is valid: ${conversion.isValid()}`);
    }
    Fiddle: Live Demo

Posting Permissions

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



Click Here to Expand Forum to Full Width