简体   繁体   English

验证代表出生日期的3个文本字段

[英]validate 3 textfields representing date of birth

I have 3 text box fields. 我有3个文本框字段。 to represent a date 代表一个日期

eg DD MM YYYY 例如DD MM YYYY

how can i validate only correct data is entered into each text box. 我如何才能验证只有正确的数据输入到每个文本框中。 is it a regexpression?? 它是一个正则表达式吗?

i need to do this inside the ascx/aspx file rather than the .cs codebehind 我需要在ascx / aspx文件而不是.cs代码背后执行此操作

thanks 谢谢

You could validate each field with regexes, but it wouldn't take into account different months with different numbers of days: you could enter invalid dates. 您可以使用正则表达式验证每个字段,但是不会考虑不同天数和不同天数的情况:您可以输入无效日期。

On the server side it could be validated with something like this: 在服务器端,可以使用类似以下内容的方法进行验证:

DateTime D;
string CombinedDate=String.Format("{0}-{1}-{2}", YearField.Text, MonthField.Text, DayField.Text);
if(DateTime.TryParseExact(CombinedDate, "yyyy-M-d", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out D)) {
  // valid
} else {
  // not valid
}

You should use CustomValidator to validate your result input of all 3 controls. 您应该使用CustomValidator来验证所有3个控件的结果输入。 Maybe inside that custom validation script you can use regex to validate data. 也许在该自定义验证脚本中,您可以使用正则表达式来验证数据。

Try putting them in to a DateTime objects. 尝试将它们放入DateTime对象。

int day, month, year;

if (Int32.TryParse(dayInput.Value, out day)) {
    if (Int32.TryParse(monthInput.Value, out month)) {
        if (Int32.TryParse(yearInput.Value, out year)) {
            DateTime birthDate = new DateTime(year, month, day);

            if ([birthDate is in valid range])
                // it passes
        }
    }
}

I know this isn't very elegant, but you can also test it the same way using the following RegEx 我知道这不是很好,但是您也可以使用以下RegEx以相同的方式对其进行测试

[0-9]+

However I like my way because I can enter it in to a DateTime field and then test the range to make sure it doesn't exceed the current date and is not below a 100 years before the current date. 但是,我喜欢我的方式,因为我可以将其输入到DateTime字段中,然后测试范围以确保它不超过当前日期并且不低于当前日期之前的100年。

Wouldn't validation in the aspx file introduce logic code in the presentation layer? aspx文件中的验证是否不会在表示层中引入逻辑代码?

I would suggest an AJAX control (there is an AJAX MaskEdit box - alike). 我建议使用AJAX控件(类似AJAX MaskEdit框)。 They usually okay for these sort of things, look into the AJAX toolkit, if the server you're deploying can support those. 如果您正在部署的服务器可以支持AJAX工具包,那么通常对于这些事情都可以接受。

Full example: 完整示例:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Web_Layer.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <script runat="server">
        protected void ValidateDate(object sender, EventArgs e)
        {
            int day=0;
            int month=0;
            int year=0;

            if (!int.TryParse(txtDD.Text, out day))
                day = 0;
            if (!int.TryParse(txtMM.Text, out month))
                month = 0;
            if (!int.TryParse(txtYY.Text, out year))
                year = 0;

            if (((year > 0)) && ((month > 0) && (month < 13)) && ((day > 0) && (day <= DateTime.DaysInMonth(year, month))))
            {
                lblValid.Text = "Valid!";
            }
            else
            {
                lblValid.Text = "NOT Valid!";
            }
        }
    </script>
    <asp:TextBox ID="txtDD" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtMM" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtYY" runat="server"></asp:TextBox>
    <asp:Button ID="btn" runat="server" OnClick="ValidateDate"/>
    <asp:Label ID="lblValid" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

那如何使用下拉菜单呢?

public bool isValidDate(string datePart, string monthPart, string yearPart)
{
    DateTime date;
    string strDate = string.Format("{0}-{1}-{2}", datePart, monthPart, yearPart);
    if (DateTime.TryParseExact(strDate, "dd-MM-yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo , System.Globalization.DateTimeStyles.None, out date ))
    {
        return true;
    }
    else
    {
        return false ;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM