mvc4 url validation
Asked Answered
P

2

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

Pitchblende answered 6/3, 2013 at 13:49 Comment(0)
L
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

Legality answered 6/3, 2013 at 14:20 Comment(3)
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.Pitchblende
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 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 onPitchblende
@Pitchblende 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@:%_\\+.~#?&//=]*)$")]Vodka
F
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")]
Fogbound answered 6/3, 2013 at 14:6 Comment(2)
This doesn't work. None of the URL regular expressions I've tried, when added as a MVC data annotation, work.Pruter
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@:%_\\+.~#?&//=]*)$")]Vodka

© 2022 - 2024 — McMap. All rights reserved.