How to make url validation without http ( or add it after validation passed )?

前端 未结 3 1987
情书的邮戳
情书的邮戳 2020-12-04 04:31

I can\'t make my custom url validation rule work.

I\'m adding new rule:

 jQuery.validator.addMethod(\"complete_url\", function(val, elem) {
// if no          


        
3条回答
  •  萌比男神i
    2020-12-04 04:55

    Please don't use regexes for this. Don't even use the same regex that the jQuery validator uses, it may change in the future and you won't be able to take advantage of the updates. This is the way to do this:

    $.validator.addMethod('validUrl', function(value, element) {
            var url = $.validator.methods.url.bind(this);
            return url(value, element) || url('http://' + value, element);
        }, 'Please enter a valid URL');
    

提交回复
热议问题