我想创建自己的验证程序,该程序检查注册表格中的PESEL是否正确。
条件:
- 如果出现错误,该字段不能为空。
- 数值应为11个数字,否则为错误。
- 如果验证功能未检查PESEL计算方法,则会返回错误。
计算PESEL的原理:
在3个简单的步骤中,我们将在下面描述如何计算支票 PESEL编号中的数字。例如,我们将使用数字 0207080362。
将PESEL编号中的每个数字乘以适当的权重: 1-3-7-9-1-3-7-9-1-3。 0 * 1 = 0 2 * 3 = 6 0 * 7 = 0 7 * 9 = 63 0 * 1 = 0 8 * 3 = 24 0 * 7 = 0 3 * 9 = 27 6 * 1 = 6 2 * 3 = 6
将获得的结果添加到您自己。请注意,如果您收到 乘法过程中的两位数,仅加最后一位 位数(例如,而不是63,请加3)。 0 + 6 + 0 + 3 + 0 + 4 + 0 + 7 +6 + 6 = 32从10减去结果。注意:如果收到 加法时为两位数,仅减去最后一位 (例如,减去32,而不是32)。您得到的数字是支票 数字。 10-2 = 8个完整的PESEL编号:02070803628
实际上它对我不起作用,我总是以我的形式这样说:
怎么了?
输入空值:
代码:
public class ValidatePesel : ValidationAttribute
{
int result_status = 0;
public string errorLenght = "Długość numeru PESEL musi zawierać 11 cyfr";
public string errorPesel = "numer PESEL jest niepoprawny";
public string errorType = "Podana wartość nie jest numerem PESEL";
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string pesel = value.ToString();
bool result_pesel = int.TryParse(pesel, out result_status);
// Verification of PESEL correctness
if (result_pesel)
{
if (pesel.Length == 11) // Check if the given length is correct
{
int[] weight = { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 }; // weights to calculate
int sum = 0;
int controlNum = int.Parse(pesel.Substring(10, 11)); // to the control value we assign the last number, the first one is the last one is 11 because 10 is the number 11 and then the length of the data
for (int i = 0; i < weight.Length; i++)
{
sum += int.Parse(pesel.Substring(i, i + 1)) * weight[i]; // we multiply each number by weight
}
sum = sum % 10; // we return the value of the checksum
if (10 - sum == controlNum)
{
return ValidationResult.Success;
}
else
{
return new ValidationResult(errorPesel);
}
}
else
return new ValidationResult(errorLenght);
}
else
return new ValidationResult(errorType);
}
}
在模型中实现的代码:
namespace Clinic. Models
{
public class RegistrationForPatient: ValidatePesel
{
public int Id {get; set; }
// We create validation of user data, if the user leaves an empty field, it returns a message from the RequiredAttribute function
[Required]
[ValidatePesel]
[Display (Name = "Pesel")]
public string PESEL {get; set; }
// Email validation, required field
[Required]
[Display (Name = "E-mail")]
[EmailAddress]
public string Email {get; set; }
[Required (ErrorMessage = "Password field, can not be empty")]
[Display (Name = "Password")]
public string Password {get; set; }
[Required (ErrorMessage = "Please repeat the password")]
[Display (Name = "Repeat password")]
public string RepeatPassword {get; set; }
}
}
答案 0 :(得分:0)
您身边的几个错误:
和工作代码:
public class ValidatePesel : ValidationAttribute
{
public string errorLenght = "Długość numeru PESEL musi zawierać 11 cyfr";
public string errorPesel = "numer PESEL jest niepoprawny";
public string errorType = "Podana wartość nie jest numerem PESEL";
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string pesel = value.ToString();
if (!decimal.TryParse(pesel, out decimal result_status)) return new ValidationResult(errorType);
if (pesel.Length == 11)
{
int[] weight = { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 };
int sum = 0;
int controlNum = int.Parse(pesel.Substring(10, 1));
for (int i = 0; i < weight.Length; i++)
{
sum += int.Parse(pesel.Substring(i, 1)) * weight[i];
}
sum = sum % 10;
if (10 - sum == controlNum) return ValidationResult.Success;
else return new ValidationResult(errorPesel);
}
else return new ValidationResult(errorLenght);
}
}