請參閱http://jsfiddle.net/gVVa8/7/jquery驗證器 - http | https | ftp | www
我想驗證http | https | ftp |萬維網。但似乎無法讓它驗證https網址。
任何人都可以幫忙嗎?
$(document).ready(function() {
// Validates http | https | ftp
// http://www.cnn.com | https://www.facebook.com | ftp://myurl.com | www.igs.org
$.validator.addMethod("cus_url", function (value, element) {
if (value.substr(0, 7) != 'http://') {
value = 'http://' + value;
}
if (value.substr(value.length - 1, 1) != '/') {
value = value + '/';
}
return this.optional(element) || /^(https|http):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i.test(value);
}, "Not valid url.");
$("#myform").validate({
ignore: [],
});
});
你的函數的第一個幾行會變成'的https :// example.com'轉換爲'http:// https:// example.com',這將不會通過您的驗證。 – Paulpro