Javascript 验证用户输入的URL
我已经在SO上检查了以前的答案。我知道他们很酷 这是我的机会: 我获取textbox的值并将其传递给函数进行验证。它正确地传递url,但不进行验证。有人能检查一下bug在哪里吗?我无法辨认Javascript 验证用户输入的URL,javascript,Javascript,我已经在SO上检查了以前的答案。我知道他们很酷 这是我的机会: 我获取textbox的值并将其传递给函数进行验证。它正确地传递url,但不进行验证。有人能检查一下bug在哪里吗?我无法辨认 <html> <head> <title>ThenWat</title> <script>@Vicky: above regex does not filter: function validateURL($UR
<html>
<head>
<title>ThenWat</title>
<script>@Vicky: above regex does not filter:
function validateURL($URL) {
alert($URL);
$pattern_1 = "/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i";
$pattern_2 = "/^(www)((\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i";
if(preg_match($pattern_1, $URL) || preg_match($pattern_2, $URL)){
alert("correct");
return true;
} else{
alert("Incorrect");
return false;
}
}
</script>
</head>
<body style="height: 560px">
<form>
<label>Enter URL: <input type="text" value="http://www.paulgraham.com/herd.html" name="sent" id="t1" style="width: 400px; height:40px;" ></label><br/>
<div style="z-index: 1; left: 420px; top: 160px; position: absolute; margin-top: 0px"> <button onclick="validateURL(document.getElementById('t1').value)";> Fire me </button>
</div>
</form>
</body>
</html>
森瓦特
@Vicky:上面的正则表达式不过滤:
函数validateURL($URL){
警报($URL);
$pattern|u 1=“/^(http | https | ftp):\/([A-Z0-9][A-Z0-9-]*)(\.[A-Z0-9][A-Z0-9-]*)+(com | org | net | dk | at | us | tv | info | uk co.uk biz | se)$)(:(((d+)/?);
$pattern|u 2=“/^(www)(\[A-Z0-9][A-Z0-9-]*)+(:(\d+))\/?/i”;
if(preg_match($pattern_1,$URL)| preg_match($pattern_2,$URL)){
警惕(“正确”);
返回true;
}否则{
警告(“不正确”);
返回false;
}
}
输入URL:
解雇我
更新
<html>
<head>
<title>ThenWat</title>
<script>@Vicky: above regex does not filter:
function validateURL($URL) {
var str=$URL;;
var n=str.match(/((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/g);
document.getElementById("demo").innerHTML=n;
//alert("correct or incorrect as per validation"); how to do this?
}
</script>
</head>
<body style="height: 560px">
<form>
<label>Enter URL: <input type="text" value="http://www.paulgraham.com/herd.html" name="sent" id="t1" style="width: 400px; height:40px;" ></label><br/>
<div style="z-index: 1; left: 420px; top: 160px; position: absolute; margin-top: 0px"> <button onclick="validateURL(document.getElementById('t1').value)";> Fire me </button>
</div>
</form>
</body>
</html>
森瓦特
@Vicky:上面的正则表达式不过滤:
函数validateURL($URL){
var str=$URL;;
var n=str.match(/((https?\:\/\/)(www\)(\S+)(\w{2,4})(:[0-9]+)(\/\/([\w?\!:+=&%@!\-\/]))?/g);
document.getElementById(“demo”).innerHTML=n;
//警报(“根据验证正确或不正确”);如何做到这一点?
}
输入URL:
解雇我
要在javascript中将字符串与正则表达式匹配,需要使用match()
。检查
URL的regexp:/((https?\:\/)(www\)(\S+)(\w{2,4})(:[0-9]+)(\/\/\/([\w\!:?+=&%@!\-\/]))?/
更新:
function myFunction(){
var str="http://www.google.com";
var regexp = /((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/g;
if(str.match(regexp)){
alert("okay");
}else{
alert("not okay");
}
}
正则表达式库:您可以使用$pattern1.test($URL)或$pattern1.match($URL),而不是使用preg\u match($pattern\u 1,$URL)您的问题在于正则表达式 我有一个非常好的,我通常会这样使用:
[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?
它首先检查是否有东西在第一个之前。(例如,testing.mywebsite.com中的测试)然后检查mywebsite和last,在最后一个点之后,它甚至会检查结尾
function validateURL($URL) {
alert($URL);
$pattern_1 = /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i;
$pattern_2 = /^(www)((\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i;
if($pattern_1.test($URL) || $pattern_2.test($URL)){
alert("correct");
return true;
} else{
alert("Incorrect");
return false;
}
}
JavaScript中没有
preg\u match
函数,除非您在其他地方定义它。您应该在
上使用onsubmit
。当用户只需在文本字段中按enter键时,也会调用该函数。preg_match函数属于php,而不是javascript。如果您有意使用php,请将函数括在php代码标记下。我在上面的正则表达式中添加了.html
,但甚至http://www.paulgraham.com/herd.html
给出的信息不正确!我的第一次测试失败我假设您无法测试url,但您的正则表达式正在工作。所以我没有测试正则表达式。有许多用于url验证的正则表达式。从“谢谢”中选择您的口味,您的脚本非常有效。我在学校编辑上查到的。但在检查了如何采取适当的行动后,如alert()或显示一些消息。你能在回答我的问题时用最新的代码吗?
function validateURL($URL) {
alert($URL);
$pattern_1 = /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i;
$pattern_2 = /^(www)((\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i;
if($pattern_1.test($URL) || $pattern_2.test($URL)){
alert("correct");
return true;
} else{
alert("Incorrect");
return false;
}
}