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

这里的情况是:我有一个非常大的文本输入框用于URL: https://asafaweb.com

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的严格定义;出于可用性的考虑,我允许没有方案的地址默认为HTTP。例如,“stackoverflow.com”将被认为是一个有效的URL。但也有一些url,出于各种原因我不想允许它们(比如它们已经被列入黑名单或者它们是内部IP地址范围)。

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 jQuery validation bundled with ASP.NET MVC expects a URL with a scheme thus breaking my schemeless URL support.

我想输入的类型设置为“url”,而不是默认的“文本”,因此用户在移动设备上得到一个键盘为url设计上下文(即iOS给你一个“。com”按钮),问题是,当我做到这一点,默认不引人注目的jQuery验证与ASP捆绑在一起。NET MVC希望有一个带有scheme的URL,从而破坏了我对无模式URL的支持。

This is an MVC4 site and I have an HTML helper like so:

这是一个MVC4网站,我有一个这样的HTML助手:

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

The ScanUrl attribute then has a custom ValidationAttribute which does all the bespoke checks to make sure that URL can be scanned.

然后,ScanUrl属性有一个定制的ValidationAttribute,它执行所有定制的检查,以确保可以扫描URL。

How can I keep the existing validation pattern without jQuery validation interjecting and wanting to make sure a URL is, well, a strict URL?

如何保持现有的验证模式,而不需要jQuery验证,并确保URL是一个严格的URL?

3 个解决方案

#1


7

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验证URL的方式可能是最简单的处理方法。您将在jquery.validate的第1034行找到它。如果你使用的是与MVC 4一起发布的版本。

You could tweak the regex right there in the plugin script, or you could patch the url validation method after jQuery Validation is loaded:

您可以在插件脚本中修改regex,也可以在加载jQuery验证之后对url验证方法进行补丁:

<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>

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.

之后对它进行补丁的主要好处是,您不必依赖于修改后的脚本版本,并且可以在以后更新jQuery验证本身的同时不会丢失定制的url验证器。这对你来说可能是显而易见的,但可能对以后找到这个答案的其他人有帮助。

更多相关文章

  1. Chrome显示错误为:由于内容安全策略,拒绝执行内联脚本
  2. 提交表单时需要运行php脚本
  3. 用无序列表键入文本脚本
  4. Python脚本利用openoffice将office文档转为html或者pdf。
  5. 8.HTML5 CSS3 背景、边框与补丁相关属性
  6. 您试图显示配置为只允许执行和脚本权限的目录中的 HTML 页
  7. 我使用生成html文件的python制作了一个脚本。如何使用Web爬网程
  8. 通过脚本自动屏蔽非法IP(转http://bbs.5y6s.com/htm_data/21/080
  9. PHP通过系统命令执行Python脚本

随机推荐

  1. Android Robotium如何管理测试用例的执行
  2. Android程序老是报错
  3. Java与C互相调用实例详解
  4. 王家林最受欢迎的一站式云计算大数据和移
  5. ffmpeg从视频中提取帧时间戳
  6. Android Web-View:将本地Javascript文件注
  7. Android各种蓝牙设备的UUID
  8. Spannable、Spanned、Editable用法及差别
  9. Androidx学习笔记(79)--- 视频播放器二(Vide
  10. Android的Intel XDK构建错误