javascript regexp, validating date problem

前端 未结 3 642
醉梦人生
醉梦人生 2020-12-22 03:59

I have some code for validating date below:


function validateForm() {
var errFound = 0;       
//var patt_date = new RegExp(\"^((((19|20)(([02468][048])|([1         


        
相关标签:
3条回答
  • 2020-12-22 04:44

    This javascript code validates date exactly. You can copy it and test it in your browser.

     var regDate = '^(19[0-9]{2}|2[0-9]{3})-(0[1-9]{1}|1[0-2]{1}){1}-(0[1-9]|(1|2)[0-9]|3[0-1]){1}$';
     var txt='2010-01-31';
     if(txt.match(regDate))
     {
        alert('date match');
     }
    
    0 讨论(0)
  • 2020-12-22 05:01

    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 讨论(0)
  • 2020-12-22 05:02

    I kinda agree with David on this... Regex matches should not be used as an exclusive criterion to decide if the passed date is, in fact, valid. The usual procedure in Javascript validation involves a few steps :

    a. The first step is to ensure that the passed string matches expected date formats by matching it against a Regex. The following may be a stricter Regex pattern.

    // Assuming that the only allowed separator is a forward slash.
    // Expected format: yyyy-mm-dd
    /^[12][90][\d][\d]-[0-3]?[\d]-[01]?[\d]$/
    

    b. The second step is to parse the string into a Date object which returns the no. of milliseconds since 1970. Use this number as a parameter for the Date constructor.

    c. Since JS automatically rolls over the passed date to the nearest valid value, you still cannot be certain if the Date object created matches that which was passed. To determine if this happened, the best way is to split the passed string according to the separator and compare individual date components:

    // d is the created Date object as explained above.
    var arrDateParts = inputDate.split("-");
    if ((d.getFullYear() == arrDateParts[0]) && (d.getMonth() == arrDateParts[1]) && (d.getDate() == arrDateParts[2]))
      return true;
    else
      return false;
    
    0 讨论(0)
提交回复
热议问题