views:

142

answers:

2

I'm trying to check whether a URL is from the same domain with jQuery 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 
            }
          }
        });
A: 

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");
Ricky
A: 

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

ilovewebdev