C#验证YYYY-MM-DD
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;//正则表达式引用的库
namespace BulletLotReport
{
public partial class frmMaintain : Form
{
public frmMaintain()
{
InitializeComponent();
}
string lot = "";
static string currentUserID = System.Environment.UserName.ToUpper();//获取当前用户ID
DAO dao = new DAO();
DataTable dt = new DataTable();
//加载后不执行
private void frmMaintain_Load(object sender, EventArgs e)
{
}
//查询变更界面数据
void ShowData(string lotids)
{
DataTable dt = dao.GetMaintainMLotList(lotids);
gc_Maintain.DataSource = dt;
}
//查询按钮逻辑
private void btnsearch_Click(object sender, EventArgs e)
{
lot = txbLotid.Text.ToString().Trim();
ShowData(lot);
}
//点击表格将行数据传给TextBox,用于Insert、Update、Delete按钮获取值
private void gv_Maintain_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
{
if (e.Button == MouseButtons.Left && e.Clicks == 2)
{
txb_Modify_Lotid.Text = gv_Maintain.GetRowCellDisplayText(e.RowHandle, "LOTID");
txb_Modify_RequiredDate.Text = gv_Maintain.GetRowCellDisplayText(e.RowHandle, "REQUIRED_DATE");
txb_Modify_Owner.Text = gv_Maintain.GetRowCellDisplayText(e.RowHandle, "OWNER");
txb_Modify_Purpose.Text = gv_Maintain.GetRowCellDisplayText(e.RowHandle, "PURPOSE");
}
}
//验证日期准确函数
public static bool ValidateDataTime(string InputStr)
{
if (InputStr.Length > 0)
{
if (Regex.IsMatch(InputStr.Trim(), @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$"))
{
return true;
}
else
{
return false;
}
}
return false;
}
//Instert按钮逻辑
private void btnInsert_Click(object sender, EventArgs e)
{
string lot = txb_Modify_Lotid.Text.ToString().Trim();
string REQUIRED_DATE = txb_Modify_RequiredDate.Text.ToString().Trim();
string OWNER = txb_Modify_Owner.Text.ToString().Trim();
string PURPOSE = txb_Modify_Purpose.Text.ToString().Trim();
//验证主键是否为空
if (lot.Equals(string.Empty))
{
MessageBox.Show("Lot IS NULL!");
return;
}
DataTable dt = dao.GetREQ_MAINTAINList(lot).Copy();
//验证数据是否已存在数据库中
if (dt.Rows.Count > 0)
{
MessageBox.Show("Lot already exist!");
}
else
{
//验证日期是否满足格式YYYY-MM-DD
string s = txb_Modify_RequiredDate.Text.ToString();
bool b = ValidateDataTime(s);
if (b)
{
//插入数据
int i = dao.InsertREQ_MAINTAIN(lot, REQUIRED_DATE, PURPOSE, OWNER, currentUserID);
if (i > 0)
{
MessageBox.Show("Insert succeeded!");
txb_Modify_Lotid.Clear();
txb_Modify_Owner.Clear();
txb_Modify_RequiredDate.Clear();
txb_Modify_Purpose.Clear();
ShowData("");
}
else
{
MessageBox.Show("Insert Failed!");
}
}
else
{
MessageBox.Show("日期格式为:YYYY-MM-DD,请输入正确的日期");
}
}
}
//Update按钮逻辑
private void btnUpdate_Click(object sender, EventArgs e)
{
string lot = txb_Modify_Lotid.Text.ToString().Trim();
string REQUIRED_DATE = txb_Modify_RequiredDate.Text.ToString().Trim();
string OWNER = txb_Modify_Owner.Text.ToString().Trim();
string PURPOSE = txb_Modify_Purpose.Text.ToString().Trim();
//验证主键是否为空
if (lot.Equals(string.Empty))
{
MessageBox.Show("Lot IS NULL!");
return;
}
//验证日期是否满足格式YYYY-MM-DD
string s = txb_Modify_RequiredDate.Text.ToString();
bool b = ValidateDataTime(s);
if (b)
{
int i = dao.UpdateREQ_MAINTAIN(lot, REQUIRED_DATE, PURPOSE, OWNER, currentUserID);
if (i > 0)
{
MessageBox.Show("Update succeeded!");
txb_Modify_Lotid.Clear();
txb_Modify_Owner.Clear();
txb_Modify_RequiredDate.Clear();
txb_Modify_Purpose.Clear();
ShowData("");
}
else
{
MessageBox.Show("Update Failed!");
}
}
else
{
MessageBox.Show("日期格式为:YYYY-MM-DD,请输入正确的日期");
}
}
//Delete按钮逻辑
private void btnDelete_Click(object sender, EventArgs e)
{
string lot = txb_Modify_Lotid.Text.ToString().Trim();
string REQUIRED_DATE = txb_Modify_RequiredDate.Text.ToString().Trim();
string OWNER = txb_Modify_Owner.Text.ToString().Trim();
string PURPOSE = txb_Modify_Purpose.Text.ToString().Trim();
//验证主键是否为空
if (lot.Equals(string.Empty))
{
MessageBox.Show("Lot IS NULL!");
return;
}
int i = dao.DeleteREQ_MAINTAIN(lot);
if (i > 0)
{
MessageBox.Show("Delete succeeded!");
txb_Modify_Lotid.Clear();
txb_Modify_Owner.Clear();
txb_Modify_RequiredDate.Clear();
txb_Modify_Purpose.Clear();
ShowData("");
}
else
{
MessageBox.Show("Delete Failed!");
}
}
}
}
`
`判断YYYY-MM-DD这种格式的,基本上把闰年和2月等的情况都考虑进去了
^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$
下面的是加了时间验证的
^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$ `