2

HTML's URL validation expects http:// or https:// by default. I tried adding a pattern attribute with custom regular expression along with type="url", but failed to do so successfully.

Is there any option that we can override the regular expression to make http:// or https:// optional?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Harun
  • 5,109
  • 4
  • 38
  • 60
  • It may be helpful in the future if you learn the regular expression syntax so these types of modifications are easy: http://www.regular-expressions.info/quickstart.html – NightOwl888 Mar 25 '15 at 15:22
  • Can you not use `[Url]` – ediblecode Mar 25 '15 at 15:36
  • 2
    Simply, something like `/some/path` is *not* a URL, so an input of `type="url"` won't accept it. You'll have to use something like `type="text"` along with the `pattern` attribute to validate via regular expression. – Chris Pratt Mar 25 '15 at 15:37
  • Have you tried the code I posted below? – Murat Yıldız Apr 01 '15 at 07:13
  • Why did some one down voted this question? I too gone through some comments like- setting type="url" for an html input has http:// or https:// optional by default. But this is not true, for me i checked in IE11 and chrome (thought it might be browser specific) and found http:// or htts:// is a mandatory prefix. But www was found optional. – Harun Apr 06 '15 at 09:24
  • @Murat Yildiz, I followed Chris Pratt's opinion by making the type="text" and set a pattern ((http(s)?://)?([\w]+\.)+[a-zA-Z]{2,4}) and that worked for me. Thanks Chris. – Harun Apr 06 '15 at 09:29
  • I thought you need a "regular expression to make http:// or https:// optional". Anyway, I voted up ChrisPratt's answer as well. – Murat Yıldız Apr 06 '15 at 09:33
  • @Murat Yildiz, Appreciate your help. I have already voted for his comment. – Harun Apr 06 '15 at 11:19

2 Answers2

2

Please try this regular expression that validates URLs without "http://", "https://" or "www":

//Validates url without "http://", "https://" or "www"
[RegularExpression(@"((www\.|(http|https|ftp|news|file|)+\:\/\/)?[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])", ErrorMessage = "Please check the url")]
TylerH
  • 20,799
  • 66
  • 75
  • 101
Murat Yıldız
  • 11,299
  • 6
  • 63
  • 63
0
  1. You can use "DataAnnotationsExtensions" library (MVC 4 URL validation). But it's too old, I don't know how it works with latest MVC versions.
  1. My solution. Not validation in model, show URL as hyperlink:

    Model:

    public class MyClass {
         public int Id { get; set; }
         public string WebSite { get; set; }
     }
    

    View:

    @foreach (var item in Model) {
     <tr>
         <td>
             <a target="_blank" href="http://@Html.DisplayFor(modelItem => item.WebSite)">@Html.DisplayFor(modelItem => item.WebSite)</a>
         </td>
         <td>
             @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
             @Html.ActionLink("Details", "Details", new { id=item.Id }) |
             @Html.ActionLink("Delete", "Delete", new { id=item.Id })
         </td>
     </tr> 
    }
    
TylerH
  • 20,799
  • 66
  • 75
  • 101