Skip to content Skip to sidebar Skip to footer

I Am Trying To Validate A Date With A Regular Expression In Javascript But It Is Not Working Correctly?

I am trying to validate a simple date with JavaScript but my no matter what date I enter it comes up false.. I am sure I am probably doing something stupid but I cannot find the so

Solution 1:

Your regex seems to work well. However, you have forgotten the date parameter in the function declaration, it may be the issue.

function validateDate(date) {
    ...
}

Oh ok, I see that you edited your question, I understand better. When you give a name attribute to an input element, you make a reference to this element. So if you want to use the value of the input named date, you have to use date.value.

I've made a jsFiddle with your code and using date.value : http://jsfiddle.net/BssTY/

Solution 2:

I've tested it on this jsFiddle as well as the regular expression itself on rubular.com, both are working with dates in the format "xx-xx-xxxx". It is failing when you're trying to use a format such as "xx-xx-xx".

Example code:

var dateCheck = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;

    if (dateCheck.test("02-03-2013") == false) {
        window.alert("Please enter a correct date");
    } else {
        window.alert("The date was entered correctly!");
    }

What exactly formats are you trying to check? Maybe you want to have a look at XDate which provides a pretty good JavaScript library for handling dates (you can check with the .valid method if a date is valid).

Solution 3:

You can add more details to limit the inputs further:

^(([1-9]|[0][0-9]|[1-2][0-9]|[3][0-1])(\/|\-)([1-9]|[0][1-9]|[1][0-2])(\/|\-)([1-9]{1}[0-9]{3}|[0-9]{2}))$

See http://rubular.com/r/uTJ55LKzMK

Breakdown the regex for checking days in the month:

([1-9]|[0][0-9]|[1-2][0-9]|[3][0-1])

- Single digit, eg. 1or2or3 up to9
- OR, can be double digits leading with0, eg. 01or02or03 up to09
- OR, can be double digits leading with1or2, eg. 10or11or22or23 up to29
- OR, can be double digits leading with3, eg. 30or31

Regex for checking month:

([1-9]|[0][1-9]|[1][0-2])
- Single digit, eg. 1or2or3 up to9
- OR, can be double digits leading with0, eg. 01or02or03 up to09
- OR, can be double digits leading with1, eg. 10or11or12

Regex for checking year:

([1-9]{1}[0-9]{3}|[0-9]{2})
- Four digits leading with# in range [1-9], eg. 1001 or 1100, up to 9999
- OR, can be double digits leading with# in range [0-9], eg. 00 or 01 up to 99

Solution 4:

There's more to validating a date than just checking the format. The OP function thinks "30-02-2013" is OK. One way to test the string is to create a date object and check against the original, e.g.

// Format dd-mm-yyyyfunctionvalidateDate(s) {
    s = s.split('-');
    var d = newDate(s[2], --s[1], s[0]);
    return !!d && d.getMonth() == s[1] && d.getDate() == s[0];
}

validateDate('30-02-2013'); // falsevalidateDate('29-02-2000'); // true

Solution 5:

use simple validation like this:

validformat=/^\d{2}\/\d{2}\/\d{4}$/

Post a Comment for "I Am Trying To Validate A Date With A Regular Expression In Javascript But It Is Not Working Correctly?"