[英]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. 我正在尝试使用JavaScript验证一个简单的日期,但是无论我输入哪个日期,它都会出现错误。.我确定自己可能做的有些愚蠢,但找不到解决方案。
<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". 我已经在jsFiddle以及rubular.com上的正则表达式中对其进行了测试 ,它们都使用格式为“ xx-xx-xxxx”的日期。 It is failing when you're trying to use a format such as "xx-xx-xx".
当您尝试使用“ 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). 也许您想看看XDate ,它提供了一个很好的JavaScript库来处理日期(您可以使用
.valid
方法检查日期是否有效)。
Your regex seems to work well. 您的正则表达式似乎运行良好。 However, you have forgotten the
date
parameter in the function declaration, it may be the issue. 但是,您忘记了函数声明中的
date
参数,这可能是问题所在。
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. 在为输入元素赋予
name
属性时,将引用此元素。 So if you want to use the value of the input named date
, you have to use date.value
. 因此,如果要使用名为
date
的输入的值,则必须使用date.value
。
I've made a jsFiddle with your code and using date.value
: http://jsfiddle.net/BssTY/ 我用您的代码并使用
date.value
了一个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 参见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.
OP功能认为“ 30-02-2013”可以。 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}$/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.