简体   繁体   中英

Regex to substring then compare

I have the below code in my mvc application:

[Required(ErrorMessageResourceName = "NumberRequired",  ErrorMessageResourceType = typeof (Messages))]
[RegularExpression("^[0-9]{16}$", ErrorMessageResourceName = "NumberRequired", ErrorMessageResourceType = typeof (Messages))]
public string number{ get; set; }

[Required(ErrorMessageResourceName = "LastFourRequired", ErrorMessageResourceType = typeof(Messages))]
[RegularExpression("^[0-9]{4}$", ErrorMessageResourceName = "LastFourRequired", ErrorMessageResourceType = typeof(Messages))]
public string lastfour{ get; set; }

My problem is i need to validate the lastfour. It must be equal to the last four digit of number. I need to do that validation in this block of code.

Can somebody help me? Thanks in advance!

First thing is, if LastFour is extract same as the last four digits of PAN , why do you need LastFour property. you can just substring that part and get the last four digits.

But if you really need to do it, You can create a custom attribute for this,

public class LastFourDigitsAttribute : ValidationAttribute, IClientValidatable
{
    private string panPropertyname;

    public LastFourDigitsAttribute(string pan)
        : base()
    {
        if (string.IsNullOrEmpty(pan))
        {
            throw new ArgumentNullException("pan");
        }

        this.panPropertyname = pan;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {
            ErrorMessage = ErrorMessage,
            // This is the name of the method added to the jQuery validator method (must be lower case)
            ValidationType = "lastfour"
        };
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value != null)
        {
            PropertyInfo panPropertyInfo = validationContext.ObjectInstance.GetType().GetProperty(panPropertyname);

            if (panPropertyInfo != null)
            {
                var panPropertyValue = panPropertyInfo.GetValue(validationContext.ObjectInstance, null);

                if (panPropertyValue != null)
                {
                    if (value.ToString() != panPropertyValue.ToString().Substring(panPropertyValue.ToString().Length - 4);)
                    {
                        return new ValidationResult(ErrorMessage);
                    }
                }
            }
        }

        return ValidationResult.Success;
    }
}

Usage,

[LastFourDigits("PAN", ErrorMessageResourceName = "CustomerEnrollment_CardLastFourInvalidMessage", ErrorMessageResourceType = typeof(Messages))]
[Required(ErrorMessageResourceName = "CustomerEnrollment_CardLastFourRequiredMessage", ErrorMessageResourceType = typeof(Messages))]
[RegularExpression("^[0-9]{4}$", ErrorMessageResourceName = "CustomerEnrollment_InvalidLastFour", ErrorMessageResourceType = typeof(Messages))]
public string LastFour { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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