问题描述
下面的情况:我在这里有一个网址一个伟大的大文本输入框:
Here's the situation: I have a great big text input box for a URL here: https://asafaweb.com
这并不需要坚持虽然URL的严格定义;我允许没有一个方案地址,则默认为HTTP可用性的目的。例如,stackoverflow.com会被认为是一个有效的URL。但也有,我不想让因各种原因(即他们已经被列入黑名单或他们的内部IP地址范围)。
This doesn't need to adhere to the strict definition of a URL though; I allow addresses without a scheme then default to HTTP for usability purposes. For example, "stackoverflow.com" would be considered a valid URL. But there are also URLs which I don't want to allow for various reasons (i.e. they've been blacklisted or they're internal IP address ranges).
我想输入的类型设置为网址,而不是默认的文字,所以在移动设备上得到用户设计的URL上下文一个键盘(即iOS的给你的.com按钮),该问题是,当我做到这一点,在ASP.NET MVC捆绑默认不显眼jQeury验证期待与这样一个方案打破我的schemeless URL支持的URL。
I want to set the type of the input to "url" rather than the default "text" so users on mobile devices get a keyboard designed for the URL context (i.e. iOS gives you a ".com" button), The problem is that as soon as I do this, the default unobtrusive jQeury validation bundled with ASP.NET MVC expects a URL with a scheme thus breaking my schemeless URL support.
这是一个MVC4的网站,我有一个HTML帮手,像这样:
This is an MVC4 site and I have an HTML helper like so:
@Html.TextBoxFor(m => m.ScanUrl, new { type = "url" })
该ScanUrl属性,那么有一个自定义ValidationAttribute它做所有的定制检查,以确保该URL进行扫描。
The ScanUrl attribute then has a custom ValidationAttribute which does all the bespoke checks to make sure that URL can be scanned.
我怎样才能保持现有的验证模式,而不jQuery验证插嘴,并希望确保一个网址,没错,是一个的严的网址是什么?
How can I keep the existing validation pattern without jQuery validation interjecting and wanting to make sure a URL is, well, a strict URL?
推荐答案
如果您不需要严格的URL验证在这个网站的任何地方,修改jQuery验证如何验证URL可以是能够处理最简单的方法。如果你使用附带的版本MVC 4,你会发现,在jquery.validate.js的1034线。
If you don't need the strict URL validation anywhere in this site, modifying how jQuery Validation validates URLs might be the easiest way to handle that. You'll find that on line 1034 of jquery.validate.js if you're using the version that ships with MVC 4.
您可以调整正则表达式正确的,在插件脚本,或者你可以修补URL验证方法后,jQuery验证已加载:
You could tweak the regex right there in the plugin script, or you could patch the url validation method after jQuery Validation is loaded:
<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>
的主要优点是修补后是,你是不依赖于脚本的调整版本,可以在不失去你的自定义URL验证以后更新jQuery验证本身。这可能是明显的给你,但可能是帮助他人后发现这个答案。
The main advantage to patching it afterward being that you aren't tied to your tweaked version of the script and could update jQuery Validation itself later without losing your customized url validator. That's probably obvious to you, but may be helpful to others finding this answer later.
这篇关于如何使用类型= QUOT; URL&QUOT;在MVC4没有jQuery的验证字段作为URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!