URL 验证器:如果未输入 http://,则动态传递它
我正在尝试为一个小型书签应用程序制作一个 URL 验证器。
我已经完成了验证器的接受操作,以便它接受应该有效的内容:
http://example.com
http://www.examples.com
http://www.example.com
ftp://www.example.com
ftp://cdn.example.com/
etc.
现在,我们中的许多人在现实生活中通常不会为网站键入 http://,所以我希望,当用户键入任何不带 http:// 的内容时:/ / 、 https:// 或 ftp:// 前面默认传递 http:// 。请注意,我不想将其传递到输入端,而是传递到后端。
因此,对于我的示例,我有这样的:
$("#in").on('keyup', function () {
var url = $('#in').val();
var match = /^(https?|ftp):\/\/(((([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})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/
if (match.test(url)) {
console.log("URL OK")
} else {
console.log("URL invalid")
}
});
如果未输入 http://,则将其传递到 console.log 以显示为有效的 url。
我不确定这是否是一个好的做法,因为它最终可能会出现许多错误,但这是我能想到的最终得到一个 http:// 网站的唯一方法。我不能强迫用户在每个书签上键入 http://。
另请注意,我不想使用任何类型的插件。
请也让我知道您对此的想法。
谢谢
I am trying to make a URL validator for a small bookmark app.
I have done the validator to accept so that it accepts what should be valid:
http://example.com
http://www.examples.com
http://www.example.com
ftp://www.example.com
ftp://cdn.example.com/
etc.
Now as many of us in real life we usually don't type http:// for a website so I want, when the user types anything without http:// , https:// or ftp:// in front to pass http:// by it default. Note that I don't want to pass it onto the input but on the backend.
So for my example I have this:
$("#in").on('keyup', function () {
var url = $('#in').val();
var match = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\amp;'\(\)\*\+,;=]|:)*@)?(((\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})|[!\amp;'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\amp;'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\amp;'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\amp;'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/
if (match.test(url)) {
console.log("URL OK")
} else {
console.log("URL invalid")
}
});
If http:// not typed, pass it on console.log to show as a valid url.
I am not sure wether this is a good practise as it could end up with many bugs but is the only way I could think of that I would end up with a http:// website. I cant force users to type http:// on every bookmark.
Also please note that I dont want to use any kind of plugins.
Please let me know your thought on this too.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的匹配有效...
实际上,如果 URL 失败,我们至少会尝试将其与第二部分进行匹配,如果它匹配但缺少协议,我们会添加它,如果一切都失败,则它是无效的。
或者像这样,在第一个匹配中使用可选协议,并在第二步中检查协议是否存在:
Assuming your match is valid...
Effectively, if the URL fails we try to match it against the second part at least, if it matches but misses the protocol we add it and if everything fails it's invalid.
Or like this, with an optional protocol in the first match and just checking for the existence of the protocol in step two:
第一:在表单提交上绑定,而不是在
keyup
上绑定在每次击键时验证 URL 的效率极低,因为您只需要在 URL 发送到服务器之前检查 URL -那是在表单提交上。因此,请改用此事件处理程序:
您的 URL 解析/验证只会在每个页面提交时运行一次,而不是每秒运行几次(取决于打字速度)。
第二:
为什么不呢? JavaScript 中的 URI 解析是一个已解决的问题。不重新发明轮子吗?
First: Bind on form submit and not on
keyup
Validating the URL on every key stroke is wildly inefficient as you only need to check the URL before it gets sent to the server - that is on form submit. So use this event handler instead:
Your URL parsing/validation will only be run once per page submit, instead of several times a second (depending on typing speed).
Second:
Why not? URI parsing in JavaScript is a solved problem. Don't re-invent the wheel?