Search code examples
regexfluentvalidation

Fluentvalidation 6.4.1.0 support me with Incorrect regex


In my case, i want to validate for url image, some url is valid but result is wrong. Eg: link image is "https://fuvitech.online/wpcontent/uploads/2021/02/bta16600brg.jpg" or "https://fuvitech.online/wp-content/uploads/2021/02/bta16-600brg.jpg" reponse "The image link is not in the correct format".

My code here:

RuleFor(product => product.Images)
            .Length(1, 3000).WithMessage(Labels.importProduct_ExceedDescription, p => ImportHelpers.GetColumnName(typeof(ProductEntity).GetProperty(nameof(p.Images))))
            .Matches(@"^(http:\/\/|https:\/\/){1}?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$").WithMessage(Labels.importProduct_UrlNotCorrect, p => ImportHelpers.GetColumnName(typeof(ProductEntity).GetProperty(nameof(p.Images))));

Please help me where the above regex is wrong. Thank you.


Solution

  • Thanks @SaSkY answer my question. I found my mistake. This source [.[a-z]{2,5}] only allows domain extensions from 2-5 characters. Example [.com] is valid. But in my case [.online] was not valid. I changed to [.[a-z]{1,10}].