简体   繁体   English

有人有 C# MVC 的出生日期验证属性吗?

[英]Anyone got a Date of Birth Validation Attribute for C# MVC?

Someone must have written this before :-)一定有人以前写过这个:-)

I need a validation attribute for date of birth that checks if the date is within a specific range - ie the user hasn't inputted a date that hasn't yet happened or is 150 years in the past.我需要一个出生日期的验证属性,用于检查日期是否在特定范围内 - 即用户尚未输入尚未发生的日期或过去 150 年的日期。

Thanks for any pointers!感谢您的任何指点!

[DateOfBirth(MinAge = 0, MaxAge = 150)]
public DateTime DateOfBirth { get; set; }

// ...

public class DateOfBirthAttribute : ValidationAttribute
{
    public int MinAge { get; set; }
    public int MaxAge { get; set; }

    public override bool IsValid(object value)
    {
        if (value == null)
            return true;

        var val = (DateTime)value;

        if (val.AddYears(MinAge) > DateTime.Now)
            return false;

        return (val.AddYears(MaxAge) > DateTime.Now);
    }
}

You could use the built-in Range attribute : 您可以使用内置的 Range属性

 
 
 
  
  [Range(typeof(DateTime), DateTime.Now.AddYears(-150).ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"), ErrorMessage = "Date of birth must be sane!")] public DateTime DateOfBirth { get; set; }
 
 

I made a Validate class where I can validate data.我创建了一个 Validate 类,我可以在其中验证数据。 In my class I have 2 methods for validating date of birth: 1 that takes a string parameter and the other takes a date parameter.在我的课程中,我有两种验证出生日期的方法:一种采用字符串参数,另一种采用日期参数。 Here is the basic method:下面是基本方法:

public static bool DateOfBirthDate(DateTime dtDOB) //assumes a valid date string
{
    int age = GetAge(dtDOB);
    if (age < 0 || age > 150) { return false; }
    return true;
}

Here is the full class:这是完整的课程:

using System;
using System.Collections.Generic;
using System.Text;

namespace YourNamespaceHere
{
    public class Validate
    {
        public static bool DateOfBirthString(string dob) //assumes a valid date string
        {
            DateTime dtDOB = DateTime.Parse(dob);
            return DateOfBirthDate(dtDOB);
        }

        public static bool DateOfBirthDate(DateTime dtDOB) //assumes a valid date
        {
            int age = GetAge(dtDOB);
            if (age < 0 || age > 150) { return false; }
            return true;
        }

        public static int GetAge(DateTime birthDate)
        {
            DateTime today = DateTime.Now;
            int age = today.Year - birthDate.Year;
            if (today.Month < birthDate.Month || (today.Month == birthDate.Month && today.Day < birthDate.Day)) { age--; }
            return age;
        }

    }
}

Here is how I'm calling the method in my Xamarin.Forms app:以下是我在 Xamarin.Forms 应用程序中调用该方法的方式:

bool valid = Validate.DateOfBirthDate(pickerDOB.Date);

Where pickerDOB is a date picker on my form.其中pickerDOB 是我表单上的日期选择器。 This makes it easy to call the validation methods from anywhere and validate a string or DateTime object.这使得从任何地方调用验证方法并验证字符串或 DateTime 对象变得容易。

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

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