How to validate a date of birth (age) if minimum is 18 in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lucfan
    New Member
    • Feb 2011
    • 2

    How to validate a date of birth (age) if minimum is 18 in C#

    I'm new in C# and my code is as follows and I need help cause I'm getting an error that says:

    The name 'date' does not exist in the current context


    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    
    public partial class ValidationIdentity : System.Web.UI.Page
    {
        
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void btnValidate_Click(object sender, EventArgs e)
        {
           
            DateTime d = DateTime.Parse(date);
    
            if (d <= DateTime.Now.AddYears(-18))
            {
                Response.Write("Citizen");
            }
            else
            {
                Response.Write("<script>window.alert('Invalid Identity Number')</script>");
            }
        
        }
            
        }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I'm not really well versed in using C# on a webpage, but the error is pretty straight forward here. You're trying to access an object, date, that doesn't exist in the current method's scope.

    If you want to get the user's age, you need to ask for it somehow. Your code that you posted doesn't show that.

    Comment

    Working...