用正则表达式实现生日输入格式的判断

时间:2025-03-09 08:49:57

 

生日格式为:yyyy—mm—dd

 

using System;
using ;
using ;
using ;
using ;
using ;
using ;

public partial class _Default :
{
    protected void Page_Load(object sender, EventArgs e)
    {
        = "";
        = "返回";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    

        //生日格式判断
        string str = ;
        int year = Convert.ToInt32((0, 3));
        int month = Convert.ToInt32((5, 2));
        string bzh = "";
        if (year/4==0)
        {
            if (!(str, "^[1-2][0-9][0-9][0-9][-](([0][2]))[-](([2][9])|([3][0-1]))$"))
            {
                bzh = "(^[1-2][0-9][0-9][0-9][-](([0][1,3,5,7,8])|([1][0,2]))[-](([0][0-9])|([1-2][0-9])|([3][0-1]))$)|(^[1-2][0-9][0-9][0-9][-](([0][4,6,9])|([1][1]))[-](([0][0-9])|([1-2][0-9])|([3][0]))$)";
            }
            else
                ("生日输入错误");
            if ((str,bzh))
            {
                ("生日输入正确");
            }
            else
                ("生日输入错误");
        }
        if (year / 4 != 0)
        {
            if (!(str, "^[1-2][0-9][0-9][0-9][-](([0][2]))[-]([3][0-1])$"))
            {
                bzh = "(^[1-2][0-9][0-9][0-9][-](([0][1,3,5,7,8])|([1][0,2]))[-](([0][0-9])|([1-2][0-9])|([3][0-1]))$)|(^[1-2][0-9][0-9][0-9][-](([0][4,6,9])|([1][1]))[-](([0][0-9])|([1-2][0-9])|([3][0]))$)";
            }
            else
                ("生日输入错误");
            if ((str, bzh))
            {
                ("生日输入正确");
            }
            else
                ("生日输入错误");
        }
           

 


    }
}