我需要用户能够创建部分网址,而无需每次都输入完整的网址。
例如,假设我计划将网址设为www.example.com/areas/{userinput}/home
。我希望用户能够在文本框中输入一些文本,并且我想验证它是否是有效的URL。
URL验证器的功能如下:
url: function(value, element) {
return this.optional(element) || /giantregex/.test(value);
}
我尝试通过以下方法向验证器添加验证方法:
$.validator.addMethod("exturl", function(value) {
return $.validator.methods.url.call($.validator, "http://www.example.com/areas/" + value + "/home");
});
但是,当验证表单并调用我的扩展名时,我不断收到此错误:
Uncaught TypeError: Object function (d,a){this.settings=b.extend(true,{},b.validator.defaults,d);this.currentForm=a;this.init()} has no method 'optional'
optional
看起来是$.validator
对象的方法,但我无法弄清楚如何调用验证器url方法。
答案 0 :(得分:16)
jQuery Validation Plugin - v1.11.0
jQuery.validator.addMethod("complete_url", function(val, elem) {
// if no url, don't do anything
if (val.length == 0) { return true; }
// if user has not entered http:// https:// or ftp:// assume they mean http://
if(!/^(https?|ftp):\/\//i.test(val)) {
val = 'http://'+val; // set both the value
$(elem).val(val); // also update the form element
}
// now check if valid url
// http://docs.jquery.com/Plugins/Validation/Methods/url
// contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
return /^(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})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(val);
});
然后:
$("#form1").validate({
rules: {
complete_url:true
}
});
来自http://www.robsearles.com/2010/05/jquery-validate-url-adding-http/
答案 1 :(得分:2)
我设法让它无误地工作,这是我使用的JS代码:
$(document).ready(function(){
function validateURL(value){
// URL validation from http://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url
var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
return value.match(regex);
}
$.validator.addMethod("urlvalidation", function(value, element) {
return this.optional(element) || validateURL(value);
}, "URL is not valid");
$('#frm_validation').validate({
rules : {
urlvalidation : { urlvalidation : true }
}
});
});
您可以在this JSFiddle上看到它。