我和我的正常EX pression一个问题。我已经能够valdiate正确瑞典社会安全号码,以符合这些标准。
YYMMDDNNNN YYMMDD-NNNN YYYYMMDDNNNN 年月日 - NNNN但我也想拒绝的用户,如果用户是18岁以下的注册。我reqular EX pression正在寻找这样的时刻:是否有人遇到同样的问题与年龄瑞典SSN
私人常量字符串RegExForValidation =
@^(\ D {6} | \ d {8}) - |(\ s){0,1} \ D {4} $;
更新
私人常量字符串RegExForValidation = @^(<日期>?\ d {6} | \ d {8})?[ - \ S] \ D {4} $ ;
字符串日期= Regex.Match(19970918-1234,RegExForValidation).Groups [日]值。
DateTime的DT;
[必需的(的ErrorMessage =杜måste昂热personnummer)
[RegularEx pression(RegExForValidation,的ErrorMessage =Personnummer安格斯配有10 siffror(yymmddnnnn))
公共字符串PersonalIdentityNumber {获得;组; }
第二次修订
公共类ValidateOfAge:ValidationAttribute
{
公共BOOL IsOfAge(DateTime的生日)
{
今天的日期时间= DateTime.Today;
INT年龄= today.Year - 18;
如果(birthdate.AddYears(birthdate.Year)< today.AddYears(-age))
返回false;
其他
返回true;
}
保护覆盖的ValidationResult的IsValid(对象的值,ValidationContext validationContext)
{
字符串RegExForValidation = @^(<日期> \ D {6} | \ d {8}?)[ - \ S] \ D {4} $?;
字符串日期= Regex.Match((串)值,RegExForValidation).Groups [日]值。
DateTime的DT;
如果(DateTime.TryParseExact(日期,新的[] {YYMMDD,年月日},CultureInfo.InvariantCulture,DateTimeStyles.None,出DT))
如果(IsOfAge(DT))
返回ValidationResult.Success;
返回新的ValidationResult(Personnummer安格斯配有10 siffror(yymmddnnnn));
}
}
解决方案
您需要从核潜艇的生日,解析为日期,然后用今天的日期进行比较。
下面的方法检查,如果一个人的年龄:
公共BOOL IsOfAge(DateTime的生日)
{
今天的日期时间= DateTime.Today; //计算时代...
INT年龄= today.Year - birthdate.Year;
如果(生日> today.AddYears(-age))
年龄 - ;
返回年龄和LT; 18?假:真的; //如果年龄是18岁以上>真,否则为假。
}
这里是如何使用这样的:
字符串RegExForValidation = @^(<日期> \ D {6} | \ d {8}?)[ - \ S] \ D {4} $?;
字符串日期= Regex.Match(19970918-1234,RegExForValidation).Groups [日]值。
DateTime的DT;
如果(DateTime.TryParseExact(日期,新的[] {YYMMDD,年月日},新的CultureInfo(SV-SE),DateTimeStyles.None,出DT))
Console.WriteLine(IsOfAge(DT));
注意 [ - |(\ S)]
匹配 -
, |
,(
,空格或)
。我相信你只想匹配一个连字号或空格。
我添加了一个名为捕捉到正则表达式并已删除字符类不必要的符号。另请注意, {0,1}
是一样的?
。
更新
要使其成为一个MVC应用程序的工作,你需要实现自定义验证:
[必需的(的ErrorMessage =杜måste昂热personnummer)
[ValidateOfAge] //< ----------------------------这里
公共字符串PersonalIdentityNumber {获得;组; }
和落实具体步骤如下:
公共类ValidateOfAge:ValidationAttribute
{
保护覆盖的ValidationResult的IsValid(对象的值,ValidationContext validationContext)
{
字符串RegExForValidation = @^(<日期> \ D {6} | \ d {8}?)[ - \ S] \ D {4} $?;
字符串日期= Regex.Match((串)值,RegExForValidation).Groups [日]值。
DateTime的DT;
如果(DateTime.TryParseExact(日期,新的[] {YYMMDD,年月日},新的CultureInfo(SV-SE),DateTimeStyles.None,出DT))
如果(IsOfAge(DT))
返回ValidationResult.Success;
返回新的ValidationResult(Personnummer安格斯配有10 siffror(yymmddnnnn));
}
}
I Have a problem with my regular expression. I have made it possible to valdiate correct swedish social security number to match these criteria.
YYMMDDNNNN YYMMDD-NNNN YYYYMMDDNNNN YYYYMMDD-NNNNBut i would also like to reject a user to register if the user is under 18 years old. My reqular expression is looking like this at the moment: Do anyone encountered the same problem with the age range Swedish SSN?
private const string RegExForValidation =
@"^(\d{6}|\d{8})[-|(\s)]{0,1}\d{4}$";
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; }
Second Update
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)");
}
}
解决方案
You need to get the birthdate from the SSN, parse to DateTime, and then compare with today's date.
Here is the method checking if a person is of age:
public bool IsOfAge(DateTime birthdate)
{
DateTime today = DateTime.Today; // Calculating age...
int age = today.Year - birthdate.Year;
if (birthdate > today.AddYears(-age))
age--;
return age < 18 ? false : true; // If the age is 18+ > true, else false.
}
And here is how you can use this:
string RegExForValidation = @"^(?<date>\d{6}|\d{8})[-\s]?\d{4}$";
string date = Regex.Match("19970918-1234", RegExForValidation).Groups["date"].Value;
DateTime dt;
if (DateTime.TryParseExact(date, new[] { "yyMMdd", "yyyyMMdd" }, new CultureInfo("sv-SE"), DateTimeStyles.None, out dt))
Console.WriteLine(IsOfAge(dt));
Note that [-|(\s)]
matches a -
, |
, (
, whitespace or )
. I am sure you only want to match a hyphen or whitespace.
I added a named capture to the regex and removed unnecessary symbols from the character class. Also, note that {0,1}
is the same as ?
.
UPDATE
To make it work in an MVC app, you need to implement a custom validator:
[Required(ErrorMessage = "Du måste ange personnummer")]
[ValidateOfAge] // <---------------------------- HERE
public string PersonalIdentityNumber { get; set; }
And implement this as follows:
public class ValidateOfAge: ValidationAttribute
{
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" }, new CultureInfo("sv-SE"), DateTimeStyles.None, out dt))
if (IsOfAge(dt))
return ValidationResult.Success;
return new ValidationResult("Personnummer anges med 10 siffror (yymmddnnnn)");
}
}