Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 154411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:58:27+00:00 2026-05-11T09:58:27+00:00

I have some code for validating date below: function validateForm() { var errFound =

  • 0

I have some code for validating date below:

 function validateForm() { var errFound = 0;        //var patt_date = new RegExp('^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30)))))$'); var patt_date = new RegExp('^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$'); if (patt_date.test(document.getElementById('datefrom').value) == false){errFound = errFound + 1;document.getElementById('datefrom').className = 'error';}  if (errFound > 0)     alert('Please correct red colored field!'); else     return true; return false;    } 

Above code should work with YYYY-MM-DD format, but fail to validate date such as ‘2009-02-29’

The commented code should work (//var patt_date = new RegExp…), it can catch ‘2009-02-29’,
but it ruin the validation when i put invalid data and try to correct it, it keeps complain there something wrong with form value after i had correct them (especially on form with multiple input)

Maybe someone can fix the current regex?


Edited, what i want just a simple replacement for above regexp, mean a new regexp pattern not the whole new method to validate date
And for reference, i simply grab the regexp pattern from:
http://www.regexlib.com/REDetails.aspx?regexp_id=694 and
http://www.regexlib.com/REDetails.aspx?regexp_id=933

Tested with 2009-02-29, 1st link work & 2nd not. Again the problem was only the 2nd regexp didn’t detect value 2009-02-29 as invalid while 1st can (but it ruin my code? so it’s must be there something wrong with it).

Thanks,
Dels

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:58:27+00:00Added an answer on May 11, 2026 at 9:58 am

    Don’t do the whole date validation with a regular expression, that’s really pushing the limits of what regexps were designed for. I would suggest this procedure instead:

    1. Check date against regexp /^\d{4}-\d{2}-\d{2}$/
    2. Extract year, month, and day using substr() and convert to integers
    3. Use some if statements to validate the integers. Like so:
         if (month == 2) {         if (day == 29) {             if (year % 4 != 0 || year % 100 == 0 && year % 400 != 0) {                 // fail             }         }         else if (day > 28) {             // fail         }     }     else if (month == 4 || month == 6 || month  == 9 || month == 11) {         if (day > 30) {             // fail         }     }     else {         if (day > 31) {             // fail     } 

    (That could certainly be written more concisely) Alternatively, you could probably perform this validation using Javascript’s Date class – you might have to do something like parsing the date, converting it back to a string, and checking if the two strings are equal. (I’m not a Javascript expert)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The obvious answer is no. BItrot does not have any… May 11, 2026 at 2:26 pm
  • added an answer You don't need to pre-compile if you don't want to.… May 11, 2026 at 2:26 pm
  • added an answer You can share an AssemblyInfo.cs file between all projects in… May 11, 2026 at 2:26 pm

Related Questions

I am currently doing some work for a company that runs a legacy web
I am trying to use the Extensibility Method Definitions from my datacontext.designer.cs file to
What are your opinions on developing for the command line first, then adding a
I went through a period of being interested in how quantum computers work and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.