26

I'm writing this question here after trying to find an answer for two days.

basically here's what's going on.

I have a property in the viewmodel as follows

[Required(ErrorMessage = "Required Field")]
[Url(ErrorMessage="Please enter a valid url")]
[DisplayName("Website")]
public string web { get; set; }

in the view, I have this

@Html.EditorFor(model => model.web, new { AutoCompleteType = "Disabled", autocomplete = "off" })

now the problem lies in how the input text for this field is validated in the client side. the field must have the protocol prefix at all times, otherwise it becomes invalid.

what is the best way I can fix this issue?

Many Thanks

Amila
  • 3,711
  • 3
  • 26
  • 42

2 Answers2

39

You can do this using the DataAnnotationsExtensions library. They have an UrlAttribute that you can configure to only validate when a protocol is specified. This attribute also supplies client-side validation. You can see an example of this behavior here: http://dataannotationsextensions.org/Url/Create

You can use this attribute as follows:

using System.ComponentModel.DataAnnotations;

namespace DataAnnotationsExtensions.Core
{
    public class UrlEntity
    {
        [Url]
        [Required]
        public string Url { get; set; }

        [Url(UrlOptions.OptionalProtocol)]
        [Required]
        public string UrlWithoutProtocolRequired { get; set; }

        [Url(UrlOptions.DisallowProtocol)]
        [Required]
        public string UrlDisallowProtocol { get; set; }
    }
}

For your purposes, the first option suffices.

The package of this library (with ASP.NET MVC support included) can be found on NuGet: Install-Package DataAnnotationsExtensions.MVC3

Note: this also works fine with ASP.NET MVC 4

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
  • 2
    Hi, thanks for your reply. I've tried to use `[Url(UrlOptions.OptionalProtocol)]`. but for the `UrlOptions` to be available, I have to add `using DataAnnotationsExtensions`, then the `Url` becomes ambiguous. to correct that, I used `[DataAnnotationsExtensions.Url(UrlOptions.OptionalProtocol)]` but it doesn't validate in the front end. – Amila Mar 07 '13 at 10:09
  • 1
    Okay, Until I find what's I'm doing wrong with DataAnnotationsExtensions, I'm going to combine answers. I grabbed the regular expression for optional protocol from [link](http://dataannotationsextensions.org/Url/Create) and used regular expression data annotation for validation. now it works fine. But i'd like to use the other annotation extensions that are provided in DataAnnotationExtensions . so have to figure out what is going on – Amila Mar 07 '13 at 13:20
  • @Amila I had the same issue. I went with using this regular exression instead. Not all encompassing but does the job `[RegularExpression("^[(http(s)?):\\/\\/(www\\.)?a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)$")]` – Lsakurifaisu Nov 11 '15 at 09:23
0

Not sure if I fully understand the question. Are you trying to validate for correctly formed URLs? If so you could implement a RegularExpression DataAnnotation as follows:

[RegularExpression(@"^http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$", ErrorMessage = "My Error Message")]
Myles J
  • 2,839
  • 3
  • 25
  • 41
  • This doesn't work. None of the URL regular expressions I've tried, when added as a MVC data annotation, work. – Mark Erasmus Sep 30 '15 at 18:56
  • 1
    This one seems to work for me [RegularExpression("^[(http(s)?):\\/\\/(www\\.)?a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)$")] – Lsakurifaisu Nov 11 '15 at 09:20