And*_*alk 5 .net c# regex asp.net-mvc regular-language
我的正则表达式有问题.我已经能够确定正确的瑞典社会安全号码以符合这些标准.
但是如果用户未满18岁,我还想拒绝用户注册.我的reqular表达现在看起来像这样:有没有人遇到与年龄范围瑞典SSN相同的问题?
private const string RegExForValidation =
@"^(\d{6}|\d{8})[-|(\s)]{0,1}\d{4}$";
Run Code Online (Sandbox Code Playgroud)
UPDATE
private const string RegExForValidation = @"^(?<date>\d{6}|\d{8})[-\s]?\d{4}$";
string date = Regex.Match("19970918-1234", RegExForValidation).Groups["date"].Value;
DateTime dt;
[Required(ErrorMessage = "Du måste ange personnummer")]
[RegularExpression(RegExForValidation, ErrorMessage = "Personnummer anges med 10 siffror (yymmddnnnn)")]
public string PersonalIdentityNumber { get; set; }
Run Code Online (Sandbox Code Playgroud)
第二次更新
public class ValidateOfAge : ValidationAttribute
{
public bool IsOfAge(DateTime birthdate)
{
DateTime today = DateTime.Today;
int age = today.Year - 18;
if (birthdate.AddYears(birthdate.Year) < today.AddYears(-age))
return false;
else
return true;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string RegExForValidation = @"^(?<date>\d{6}|\d{8})[-\s]?\d{4}$";
string date = Regex.Match((string)value, RegExForValidation).Groups["date"].Value;
DateTime dt;
if (DateTime.TryParseExact(date, new[] { "yyMMdd", "yyyyMMdd" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
if (IsOfAge(dt))
return ValidationResult.Success;
return new ValidationResult("Personnummer anges med 10 siffror (yymmddnnnn)");
}
}
Run Code Online (Sandbox Code Playgroud)
您需要从 SSN 获取生日,解析为 DateTime,然后与今天的日期进行比较。
\n\n这是检查一个人是否年龄的方法:
\n\npublic bool IsOfAge(DateTime birthdate)\n{\n DateTime today = DateTime.Today; // Calculating age...\n int age = today.Year - birthdate.Year;\n if (birthdate > today.AddYears(-age)) \n age--;\n return age < 18 ? false : true; // If the age is 18+ > true, else false.\n}\nRun Code Online (Sandbox Code Playgroud)\n\n以下是如何使用它:
\n\nstring RegExForValidation = @"^(?<date>\\d{6}|\\d{8})[-\\s]?\\d{4}$";\nstring date = Regex.Match("19970918-1234", RegExForValidation).Groups["date"].Value;\nDateTime dt;\nif (DateTime.TryParseExact(date, new[] { "yyMMdd", "yyyyMMdd" }, new CultureInfo("sv-SE"), DateTimeStyles.None, out dt))\n Console.WriteLine(IsOfAge(dt));\nRun Code Online (Sandbox Code Playgroud)\n\n请注意,[-|(\\s)]匹配-、|、(、 空格或)。我确信您只想匹配连字符或空格。
我向正则表达式添加了一个命名捕获,并从字符类中删除了不必要的符号。另请注意,{0,1}与?。
更新
\n\n为了使其在 MVC 应用程序中工作,您需要实现一个自定义验证器:
\n\n[Required(ErrorMessage = "Du m\xc3\xa5ste ange personnummer")]\n[ValidateOfAge] // <---------------------------- HERE\npublic string PersonalIdentityNumber { get; set; }\nRun Code Online (Sandbox Code Playgroud)\n\n并按如下方式实现:
\n\npublic class ValidateOfAge: ValidationAttribute\n{\n protected override ValidationResult IsValid(object value, ValidationContext validationContext)\n { \n string RegExForValidation = @"^(?<date>\\d{6}|\\d{8})[-\\s]?\\d{4}$";\n string date = Regex.Match((string)value, RegExForValidation).Groups["date"].Value;\n DateTime dt;\n if (DateTime.TryParseExact(date, new[] { "yyMMdd", "yyyyMMdd" }, new CultureInfo("sv-SE"), DateTimeStyles.None, out dt))\n if (IsOfAge(dt))\n return ValidationResult.Success;\n return new ValidationResult("Personnummer anges med 10 siffror (yymmddnnnn)");\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1979 次 |
| 最近记录: |