Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:37:47+00:00 2026-06-09T11:37:47+00:00

I have several views in an MVC3 project that allow a user to enter

  • 0

I have several views in an MVC3 project that allow a user to enter a Url into a text input and save it as data on an entity.

We need to validate that the input is a validly formed Url.

I first started with using the [Url] annotation from http://dataannotationsextensions.org/ but for some strange reason, the client-side validation fails if the user inputs any uppercase characters. So to alleviate that I added a jQuery event that forces the input to lowercase as the user types. Turns out this isn’t optimal either because the user can paste in a Url that has been shortened by a Url shortening service such as tinyUrl, etc. and those uppercase characters are necessary for the translation.

So I went instead with a regular expression:

[RegularExpression(@"^((ht|f)tp(s?)\:\/\/|~/|/)?([\w]+:\w+@)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?((/?\w+/)+|/?)(\w+\.[\w]{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?", ErrorMessage = "Please enter a valid Url")]

This works fine for allowing uppercase characters. But it’s not perfect. With this Regex these are all considered valid:

  1. http://www.asp
  2. asp.net
  3. http://asp.net
  4. http://www.asp.net
  5. http://www.asp

As you can see, some of these aren’t truly valid.

The biggest problem with these Urls is when displaying the Url back in a Display View. If the Url text does not have the http:// prepended to the Url then the link that is created in the View ends up looking like this:

http://www.mydomain.com/controller/action/www.asp 

or

http://www.mydomain.com/controller/action/asp.net

which are both invalid obviously.

For example, this bit of Razor code will create a link like the two above if the Url does not have http:// prepended

....
<a href="@item.Link" target="blank">@item.LinkName</a>
...

But if the entered Url has http:// prepended the Url generated in the display view actually is a real Url such as

http://www.asp.net
http://asp.net
http://www.asp - this is actually an invalid address as well but it renders out correctly.

Surely, showing a Url that was saved into a db and then shown on a View isn’t as difficult as this and also allowing a user to save a well formed Url but also abbreviating it such as:

asp.net
www.asp.net

without having to include the prepended protocol.

Update

Here is the definition of my Url field in my ViewModel using the annotation from http://dataannotationsextensions.org/

[Required]
[StringLength(128)]
[Url(false)] // false denotes whether the protocol is required or not
public string URL { get; set; }

So you don’t think I’m nuts, here is a screenshot using uppercase and no uppercase. Tested in IE8 and Chrome so far and I get the same results

Screenshot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:37:51+00:00Added an answer on June 9, 2026 at 11:37 am

    Solved, the 2 DLLs DataAnnotationsExtensions.dll and DataAnnotationsExtensions.ClientValidation.dll from http://dataannotationsextensions.org/ that I was using were version 1.0 and did not allow for uppercasing in the [Url] annotation. I updated to version 1.1 on both as this is the version demo’d on the website and the problem is now fixed.

    Using [Url] will now allow me to enforce http:// if I absolutely have to as well as allowing uppercase characters.

    Using [Url(false)] will allow the user to not have to enter http:// and I can prepend it in code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few views that each have several XAML TextBox instances. The Text
I have Django project on Dreamhost server which has several views that returns Json
I have several views that have common code I'd like to abstract into a
I have an app that have several views. In one view I would like
I have a view controller based app that contains several views that I display/hide
i have user control, which i render on several views. i want to show
I have an application with several windows/views that show the same object. For example
I have a window that contains several rather complex views. Right now, I'm using
I have several partial views with Javascript that I am trying to move to
I'm developing a new desktop application that will have several views such as a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.