简体   繁体   中英

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 solution.

<script type="text/javascript">
/* <![CDATA[ */
function validateDate(date) {
var dateCheck = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
    if (dateCheck.test(date) == false) {
        window.alert("Please enter a correct date");
    }
    else {
    window.alert("The date was entered correctly!");
    }
}
/* ]]> */
</script>


Please enter a date:
input type='text' name='date' id='date'>
input type='button' name="submitDate" id='submitDate' value='Submit' onclick="validateDate()">

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).

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/

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. 1 or 2 or 3 up to 9
- OR, can be double digits leading with 0, eg. 01 or 02 or 03 up to 09
- OR, can be double digits leading with 1 or 2, eg. 10 or 11 or 22 or 23 up to 29
- OR, can be double digits leading with 3, eg. 30 or 31

Regex for checking month :

([1-9]|[0][1-9]|[1][0-2])
- Single digit, eg. 1 or 2 or 3 up to 9
- OR, can be double digits leading with 0, eg. 01 or 02 or 03 up to 09
- OR, can be double digits leading with 1, eg. 10 or 11 or 12

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

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, eg

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

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

使用像这样的简单验证:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM