Home  >  Article  >  Web Front-end  >  JavaScript code to determine the correctness of input dates in two formats_javascript skills

JavaScript code to determine the correctness of input dates in two formats_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:16:01996browse
The simplest
Copy code The code is as follows:

function isValidDate( dateStr) { 
 var matchArray = dateStr.match(/^[0-9] -[0-1][0-9]-[0-3][0-9]$/)
if ( matchArray == null) {
alert("Invalid date: " dateStr);
return false;
}
return true;
} }
function isValidDate(dateStr) {
var matchArray = dateStr.match(/^[0-9] -[0-1][0-9]-[0-3][0-9]$/)
if (matchArray == null) {
alert("Invalid date: " dateStr);
return false;
return true; }


Second type





The third type is more complicated

< ;script language=javascript>
String.prototype.isTime = function()
{
var r = this.match(/^(d{1,4})(-|/)(d{ 1,2})2(d{1,2}) (d{1,2}):(d{1,2}):(d{1,2})$/); ==null)return false; var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
return (d.getFullYear()==r[1]&&(d.getMonth() 1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d .getMinutes()==r[6]&&d.getSeconds()==r[7]);
}
alert("2002-1-31 12:34:56".isTime());
alert("2001-2-29 12:54:56".isTime());
alert("2002-1-41 12:00:00".isTime());
< ;/script>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Non-regular implementation of an input box that can only input Chinese characters_javascript skillsNext article:Non-regular implementation of an input box that can only input Chinese characters_javascript skills

Related articles

See more