I'm using ASP.NET MVC3 and trying to validate an URL field using DataAnnotationsExtensions.
It's almost what I need. However, it forces the user to add "http://" at the beggining of the URL string, if not, it will show the following validation message:
The URL field is not a valid fully-qualified http, https, or ftp URL.
In the Data Annotations Extensions URL demo page it shows an additional validator UrlWithoutProtocolRequired, but I cannot find it anywhere.
How can I use this validator, or how can I easily validate the URL without the "http://" part?
ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process.
In Asp.net MVC, we can easily apply validation to web application by using Data Annotation attribute classes to model class. Data Annotation attribute classes are present in System.
The protocol-less option for DataAnnotationsExtensions is available in the source code but is considered beta or "vNext" and hasn't been released as part of the NuGet package. So if you download the source and compile you'll see the [Url] attribute has an overload [Url(requireProtocol: false)]. You can see this in the latest UrlAttribute.cs file (UrlArribute.cs). Also if you look in the DataAnnotationsExtensions wiki you'll see this feature is scheduled to be released soon (I'm thinking in the next week or two for an official next release).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With