Can't get why URL custom validation method for

2019-07-28 22:45发布

问题:

I'm trying to validate URL. I want to make these URLs pass the validation:

    www.site.com

    www.super.co.uk

I'm checking my RegEx in this site - http://www.rubular.com/

Here is my RegEx:

    /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/

It is working in Rubular site, but when I'm trying to use it in my custom jQuery validation Plugin method - it constantly tells me, that it is invalid:

     jQuery.validator.addMethod("complete_url", function(val, elem) {
// if no url, don't do anything
if (val.length == 0) { return true; }

    return /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test(val);
   });

   ...
   rules: {
         'website[url]': {
            required: true,
            url:  "complete_url",
            remote: {
            url: "http://127.0.0.1:3000/checkurl"
                  //url: "http://91.228.126.168:3000/checkurl"
            }
         }
        }

回答1:

/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test("www.super.co.uk"); returns true in chrome's console, so it is probably something with your plugin.