简体   繁体   中英

validate date of birth in MVC asp.net

i'm having difficulties to validate date of birth. the user should select a date from the calendar. once the user click on validate. it should be able to determine if it is over 18 or under 18. im new in mvc. any help? what statement should i insert to validate the dob (under or over 18) here what i have tried: Controller:

public ActionResult Create(Information information, string buttonType)
    {
        if (buttonType=="Register")
        {
            var byteArray = Encoding.ASCII.GetBytes(information.Surname + Environment.NewLine + information.DOBP + Environment.NewLine + information.Email + Environment.NewLine + information.Gender + Environment.NewLine + information.Tel);
            var stream = new MemoryStream(byteArray);
            return File(stream, "text/plain", "Registration.txt");
        }
        if (buttonType=="Validate")
        {


        }
        return View(information);



    }

您可以使用 Javascript 执行相同操作。检查链接链接以进行 javascript 检查。检查值后,您可以在浏览器本身中进行验证。为什么要在后面的代码中执行此类操作?是否需要此类活动来执行后面的代码?

Bhav

Here is a code example I am using some where in m code. Hope it helps you.

public bool IsValidDOB(DateTime date)
    {
        bool  objResult = null;
        try
        {
            if ( date < System.DateTime.Now )
            {
                DateTime now = DateTime.Today;
                int age = now.Year - date.Year;
                if (date > now.AddYears(-age)) age--;
                if (age>=18)
                objResult =  true;
                else
                    objResult =  false;
            }
            else
            {
                objResult =  false;
            }
        }
        catch (Exception ex)
        {
            objResult = new  false;
            Core.Logger.AdminTrace.Logger(Core.Logger.LogArea.BusinessTier, ex);
        }
        return objResult;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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