jquery - jQuery:检查日期格式是否有效
问题描述
如何使用 jQuery 检查日期格式是否有效?
01/23/2017 --> true
23/01/2017 --> true
23-01-2017 --> true
01/23 --> true
23/01 --> true
23-01 --> true
23/23 --> false
23-23 --> false
解决方案
请看答案。在自定义中验证输入日期的最简单方法
$(document).ready(function () {
$('#btn_move').click(function () {
var dateformat = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-]|1[012])\/\-]\d{4}$/;
var Val_date = $('#txt_date').val();
if (Val_date.match(dateformat))
{
var seperator1 = Val_date.split('/');
var seperator2 = Val_date.split('-');
if (seperator1.length > 1)
{
var splitdate = Val_date.split('/');
}
else if (seperator2.length > 1)
{
var splitdate = Val_date.split('-');
}
var dd = parseInt(splitdate[0]);
var mm = parseInt(splitdate[1]);
var yy = parseInt(splitdate[2]);
var ListofDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (mm == 1 || mm > 2)
{
if (dd > ListofDays[mm - 1])
{
alert('Invalid date format!');
return false;
}
}
if (mm == 2)
{
var lyear = false;
if ((!(yy % 4) && yy % 100) || !(yy % 400))
{
lyear = true;
}
if ((lyear == false) && (dd >= 29))
{
alert('Invalid date format!');
return false;
}
if ((lyear == true) && (dd > 29))
{
alert('Invalid date format!');
return false;
}
}
}
else
{
alert("Invalid date format!");
return false;
}
});
});
推荐阅读
- elasticsearch - 索引清理和保留 Elastic Cloud
- android - 是否有用于多平台预资助礼品代码生成的 API/平台
- dynamics-crm - Xrm.Page.getAttribute(...).getValue[0].id 的不同 GUID 格式
- php - 显示分类并使用已分配帖子的缩略图
- reactjs - 使用反应组件渲染图
- react-native - 找不到 TurboModuleRegistry.getEnforcing 'Networking'。验证此名称的模块是否已在本机二进制文件中注册
- ios - 使用 appcenter 在 ios 应用程序上推送通知未成功
- django - 尝试删除模型实例时出现无效的列名错误
- c# - Regex.Split 考虑字母和破折号
- android - 从 Editext 获取输入