开发者

jQuery Validation addMethod - Check if same domain

I'm trying to check whether a URL is from the same domain with jQuer开发者_如何学运维y Validation addMethod.

Here's what i got so far, but it doesn't seem to be working:

jQuery.validator.addMethod("domain", function(value, element) { 
            return this.optional(element) || /^http:\/\/testsite.com/.test(value); 
        }, "The URL doesn't match.");

        $("#url_form").validate({
          rules: {
            url: {
              required: true,
              url: true,
              domain : true 
            }
          }
        });


Got it figured out. Might be a better way to do, but this worked for me:

jQuery.validator.addMethod("domain", function(value, element) { 
            return this.optional(element) || /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)testsite.com/.test(value); 
        }, "The URL must be on the same Site domain");


I use parseUri for all my url's comparison. Might be useful in your case.


I have made a modification on jquery.validade.js, to accept a url without http://. Please download it from http://www.tream.com.br/jquery.validate.js.

Usage:

url: true

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜