我已经检查了之前关于 SO 的答案。我知道他们很酷。
这是我的机会:我正在获取文本框的值并将其传递给函数进行验证。它正确传递 url 但不进行验证。有人可以检查错误在哪里吗?我无法识别
<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>
更新
<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>