2010-09-04 85 views
2

我在asp.net工作,我想從文本框中的用戶採取DOB,我如何驗證feb和閏年考慮的測試箱。我還希望在DD中輸入日期/ MM/YYYY。出生日期驗證

回答

1

試試這個 -

// <summary> 
// Determine if Date String is an actual date 
// Date format = MM/DD/YYYY 
// </summary> 
// <param name="date"></param> 
// <returns></returns> 
private bool ValidateDate(string date) 
{ 
    try 
    { 
     // for US, alter to suit if splitting on hyphen, comma, etc. 
     string[] dateParts = date.Split('/'); 

     // create new date from the parts; if this does not fail 
     // the method will return true and the date is valid 
     DateTime testDate = new 
      DateTime(Convert.ToInt32(dateParts[2]), 
      Convert.ToInt32(dateParts[0]), 
      Convert.ToInt32(dateParts[1])); 

     return true; 
    } 
    catch 
    { 
     // if a test date cannot be created, the 
     // method will return false 
     return false; 
    } 
} 
+0

當存在測試日期字符串的現有方法時拋出異常似乎不雅觀。 – RedFilter 2010-09-13 18:34:06

1

嘗試使用正則表達式驗證在ASP.NET ..事情是這樣的:

<asp:RegularExpressionValidator ID="RegExpVal1" runat="server" ControlToValidate="{Here Goes your Text Box}" ErrorMessage="DOB has to be in dd/mm/yyyy format" ValidationExpression="^([1-9]0[1-9][12][0-9]3[01])[- /.]([1-9]0[1-9]1[012])[- /.][0-9]{4}$" >*</asp:RegularExpressionValidator> 

編輯:我missunderstood你的問題(TNX到@rob)。所以也許你可以使用自定義驗證器。類似這樣的:

<asp:CustomValidator ID="MyValidator" runat="server" 
    ErrorMessage="Invalid date!" 
    ControlToValidate="tbDOB" 
    OnServerValidate="MyValidator_ServerValidate"> 
</asp:CustomValidator> 

將以上添加到您的DateTime用戶控件。現在檢查有效日期:

public partial class DateTime_EditField : System.Web.DynamicData.FieldTemplateUserControl 
    { 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void MyValidator_ServerValidate(object source, ServerValidateEventArgs args) 
    { 
     DateTime temp; 
     String textDate = tbDOB.Text; 

     if (DateTime.TryParse(textDate, out temp)) 
     { 
      args.IsValid = true; 
     } 
     else 
     { 
      args.IsValid = false; 
     } 
    } 
} 
+1

-1,對不起,這不會在OP詢問時處理'驗證測試框是否符合feb和閏年考慮。 – Rob 2010-09-04 09:20:25

+1

@Rob:夠公平的...... – rjovic 2010-09-04 09:22:27

1

您可以使用DateTime.TryParseExact

string dateString = "22/07/1876"; 
DateTime dateOut; 
if ((DateTime.TryParseExact(dateString, "dd/MM/yyyy", 
        new CultureInfo("en-US"), 
        DateTimeStyles.None, 
        out dateOut))) 
    Console.WriteLine("Date is valid: {0}", dateOut.ToString()); 
else 
    Console.WriteLine("Date string {0} is invalid.", dateString); 
1

做的最好的事情是使用DateTime.ParseDate方法:

var date = "01/10/1981"; 

var parsedDate = new DateTime(); 
if (DateTime.TryParse(date, out parsedDate)) 
{ 
    // Date is valid and is now in "date" 
} 
else 
{ 
    // Date is NOT valid/parseable 
} 
2

這些瓦利代碼代碼也起作用。

<asp:CompareValidator ErrorMessage="(mm/dd/yyyy)" Display="Dynamic" ID="valcDate" ControlToValidate="txtDate" Operator="DataTypeCheck" Type="Date" runat="server"></asp:CompareValidator>   

<asp:RangeValidator ID="valrDate" runat="server" ControlToValidate="txtDate" MinimumValue="12/31/1950" MaximumValue="1/1/2100" Type="Date" text="Invalid Date" Display="Dynamic"/>