How can you use type = "url" in MVC4 without jQuery, checking this field as a URL?

Here's the situation: I have a large text input box for the URL here: https://asafaweb.com

It is not necessary to adhere to a strict definition of the URL; I allow addresses without a scheme, and the default is HTTP for ease of use. For example, "stackoverflow.com" would be considered a valid URL. But there are also URLs that I don’t want to resolve for various reasons (i.e., they were blacklisted or they are internal ranges of IP addresses).

I want the input type to be “url” and not the default “text”, so users on mobile devices get a keyboard designed for the context of the URL (for example, iOS gives you the “.com” button), The problem is that as soon as I do this, the standard unobtrusive jQuery validation associated with ASP.NET MVC expects a URL with a schema, thereby breaking my contactless URL support.

This is an MVC4 site, and I have an HTML helper:

@Html.TextBoxFor(m => m.ScanUrl, new { type = "url" })

The ScanUrl attribute then has a custom ValidationAttribute attribute that executes all validated requests to make sure the URL can be crawled.

How can I save an existing validation pattern without jQuery validation in the assumption and want to make sure that the url is, well, a strong url?

+5
3

URL- , URL- . 1034 jquery.validate.js, , MVC 4.

, script, URL jQuery Validation:

<script src="/Scripts/jquery.validate.js"></script>
<script>
  // To no-op url validation completely.
  jQuery.validator.methods.url = function(value, element) {
    return this.optional(element) || true;
  };

  // Or, continue validating everything else as previously, 
  //  but not require the protocol (double check my change to the regex...).
  jQuery.validator.methods.url = function(value, element) {
    return this.optional(element) || /^(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
  };    
</script>

, script jQuery, URL. , , , , .

+7

javascript URL- , /.

0

jQuery Validation , , , , class="ignorejQueryValidate" , type="url".

ignore jQuery docs Validation validate() . validate().

@using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Post, new { id = "myForm"}))
{
    @Html.TextBoxFor(m => m.ScanUrl, new { type = "url", @class = "ignorejQueryValidate" })
}

$("#myForm").validate({
    ignore: ".ignorejQueryValidate"
});

@using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Post, new { id = "myForm" }))
{
    @Html.TextBoxFor(m => m.ScanUrl, new { type = "url" })
}

$("#myForm").validate({
    ignore: "input[type=url]"
});
0

All Articles