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

 

生日格式为:yyyy—mm—dd

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        link.HRef = "http://baidu.com";
        link.InnerText = "返回";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    

        //生日格式判断
        string str = TextBox3.Text;
        int year = Convert.ToInt32(str.Substring(0, 3));
        int month = Convert.ToInt32(str.Substring(5, 2));
        string bzh = "";
        if (year/4==0)
        {
            if (!Regex.IsMatch(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
                Response.Write("生日输入错误");
            if (Regex.IsMatch(str,bzh))
            {
                Response.Write("生日输入正确");
            }
            else
                Response.Write("生日输入错误");
        }
        if (year / 4 != 0)
        {
            if (!Regex.IsMatch(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
                Response.Write("生日输入错误");
            if (Regex.IsMatch(str, bzh))
            {
                Response.Write("生日输入正确");
            }
            else
                Response.Write("生日输入错误");
        }
           

 


    }
}

你可能感兴趣的:(用正则表达式实现生日输入格式的判断)