Exemple #1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
            {
                return(null);
            }
            var nhsNumber = NotificationFieldFormattingHelper.FormatNhsNumberForModel(value.ToString());
            var match     = Regex.Match(nhsNumber, "^[0-9]+$");

            if (!match.Success)
            {
                return(new ValidationResult(string.Format(ValidationMessages.NumberFormat, validationContext.DisplayName)));
            }
            else if (nhsNumber.Length != 10)
            {
                return(new ValidationResult(ValidationMessages.NhsNumberLength));
            }

            var monodigitNumbers = new List <string> {
                "0000000000", "1111111111", "9999999999"
            };

            if (monodigitNumbers.Contains(nhsNumber))
            {
                return(new ValidationResult(ValidationMessages.NhsNumberMonodigit));
            }

            var firstDigit = nhsNumber.Substring(0, 1);
            // Scotland uses different validation and has NHS numbers starting with 0, 1 and 2. 9 is a generally used test digit for NHS numbers.
            var scottishAndTestDigits = new List <string> {
                "0", "1", "2", "9"
            };

            if (scottishAndTestDigits.Contains(firstDigit))
            {
                return(null);
            }

            if (!ValidateNhsNumber(nhsNumber))
            {
                return(new ValidationResult(ValidationMessages.InvalidNhsNumber));
            }

            return(null);
        }
        public async Task <JsonResult> OnPostNhsNumberDuplicates([FromBody] NhsNumberValidationModel validationData)
        {
            var group = await NotificationRepository.GetNotificationGroupAsync(validationData.NotificationId);

            return(new JsonResult(await GenerateDuplicateNhsNumberNotificationUrlsAsync(NotificationFieldFormattingHelper.FormatNhsNumberForModel(validationData.NhsNumber), group)));
        }