如何验证输入的日期格式是否有效?

时间:2016-03-17 11:53:27

标签: javascript

如何检查输入日期格式是否有效在文本框$(“#txtCustomDate”)中。

function datVal(){
  var obj = $("#txtCustomDate").val();
  var format = obj.toUpperCase();
  var date = new Date();
  text = moment(date).format(format);
  $('#editor').val(value + "<span>" + text + "</span>");
}

1 个答案:

答案 0 :(得分:-1)

var dateExp= /^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-.\/])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
if(dateExp.test('17-03-2016'))
{
	alert('Valid Format');
}
else
{
	alert('Invalid Format');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>