使用jQuery验证URL并添加http://和final/(如果需要) [英] Validate a URL with jQuery and add http:// and final / if needed
问题描述
我正在网站的后端中做一个简单的URL构建器,我需要一个 http://www .google.es/,其中需要 http://和/,但用户通常输入www.google.es或maxmium
I am doing a simple URL Builder in a backend of a website where I need a url like http://www.google.es/ where http:// and / are needed but the users usually inputs www.google.es or maxmium http://www.google.es.
I have this basic code where I check for not blank values and for a valid url:
$("#ga-generate").click(function(){
var url = $("#ga-url").val();
var nombre = $("#ga-nombre").val();
if(url == "") {
alert("URL is required");
return false;
}
else {
//set the new URL with http and / if needed
$("#ga-url").val(url);
}
if(nombre == "") {
alert("A name is required");
return false;
}
if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\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])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\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})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([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})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([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(url)) {
var final_url = "";
final_url = url;
}
else {
alert("URL is not valid");
}
});
正则表达式检查有效的URL,它检查它是否具有 http://(而不是final/),所以我想添加http://和/(如果没有)在调用正则表达式之前添加.我该怎么办?
The regex checks for a valid URL, it checks if it have http:// (and not a final / ) so I would like to add the http:// and / if it's not added just before call the regex. How I can do it?
先谢谢您!
编辑:在第一部分中,添加http,其中有一个PHP函数,我不知道如何将其转换为javascript:
EDIT: for the first part, add http, there is a PHP function that I don't know how to translate to javascript:
function addhttp($url) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}
推荐答案
您要的是后端还是前端?您声明在后端需要它,但是问题仅用jquery标记. 另外,无论用户是在后端执行此检查,因为用户可能会绕过javascript
Did you want this is backend or frontend? You state you need it in backend, but question is only tagged with jquery. Also, you should be doing this check is backend regardless, since users can possibly circumvent javascript
if(url.substr(0,7) != 'http://'){
url = 'http://' + url;
}
if(url.substr(url.length-1, 1) != '/'){
url = url + '/';
}
这篇关于使用jQuery验证URL并添加http://和final/(如果需要)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!