Winform之TextBox输入日期格式验证yyyy-mm-dd
时间:2023-04-14 21:35:05
private void button1_Click(object sender, EventArgs e)
{
string s = textBox1.Text.ToString();
bool b = ValidateDataTime(s);
string rs = "";
if (b)
{
rs = "true";
}
else
{
rs = "false";
}
MessageBox.Show(rs);
}
//验证方法-格式为:YYYY-MM-DD
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;
}
判断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$
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android Studio做超好玩的拼图游戏 附送详细注释源码
![](https://img.aspxhome.com/file/2023/4/84744_0s.jpg)
C#多线程系列之线程的创建和生命周期
![](https://img.aspxhome.com/file/2023/2/77262_0s.jpg)
Android开发之利用jsoup解析HTML页面的方法
![](https://img.aspxhome.com/file/2023/6/138536_0s.png)
Android触屏测试实例代码
Java Calendar类使用案例详解
C#实现redis读写的方法
![](https://img.aspxhome.com/file/2023/1/108631_0s.jpg)
MyBatis中${} 和 #{} 有什么区别小结
![](https://img.aspxhome.com/file/2023/2/89572_0s.jpg)
Android 3D旋转动画效果实现分解
![](https://img.aspxhome.com/file/2023/8/129028_0s.gif)
Android Build Variants 为项目设置变种版本的方法
![](https://img.aspxhome.com/file/2023/1/89541_0s.jpg)
Android实现背景可滑动登录界面 (不压缩背景弹出键盘)
![](https://img.aspxhome.com/file/2023/9/138469_0s.gif)
C#中TreeView实现适合两级节点的选中节点方法
C#浅拷贝和深拷贝实例解析
![](https://img.aspxhome.com/file/2023/5/100135_0s.png)
Springboot集成RabbitMQ死信队列的实现
![](https://img.aspxhome.com/file/2023/9/77479_0s.png)
Hibernate批量处理海量数据的方法
IDEA离线安装maven helper插件的图文教程
![](https://img.aspxhome.com/file/2023/3/78053_0s.jpg)
Android Activity与Intent详解及示例代码
![](https://img.aspxhome.com/file/2023/8/96018_0s.gif)
详解Java程序启动时-D指定参数是什么
![](https://img.aspxhome.com/file/2023/9/61559_0s.png)
Android实现图片点击放大
C语言实现两个矩阵相乘
![](https://img.aspxhome.com/file/2023/6/103076_0s.png)
关于springboot响应式编程整合webFlux的问题
![](https://img.aspxhome.com/file/2023/0/68710_0s.png)