-1

I have a form running with jquery validate. The url methode they are using require http to be added to the url, and I want to allow users to submit url without http and or www. I used add methode:

   jQuery.validator.addMethod("urlRegex", function(value, element) {
        return this.optional(element) || /^(?:([a-z]+):(?:([a-z]*):)?\/\/)?(?:([^:@]*)(?::([^:@]*))?@)?((?:[a-z0-9_-]+\.)+[a-z]{2,}|localhost|(?:(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.){3}(?:(?:[01]?\d\d?|2[0-4]\d|25[0-5])))(?::(\d+))?(?:([^:\?\#]+))?(?:\?([^\#]+))?(?:\#([^\s]+))?$/.test(value);
    }, "PLEASE ENTER A VAILD WEBSITE ");

and under the rules I added the new method:

rules: {
            name: {
                required: true,
                minlength: 2
            },

            email: {
                required: true,
                email: true
            },

            website: {
                required: true,
                urlRegex: true
            },
         }

But it's still require http:\ at the beginning of the url. I checked this regex at regex101.com and it matches with urls such as example.com so I'm not sure if the problem is with the regex or with something else.

Thanks.

Sparky
  • 98,165
  • 25
  • 199
  • 285
Noam Gur
  • 65
  • 11

1 Answers1

0

Use this instead...

This will work with or without HTTP://

/^([\:\/\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$
TheValyreanGroup
  • 3,554
  • 2
  • 12
  • 30
  • Hey, Thanks. As iv'e wrote in the question, I need this to also work without www.(example.com should be valid). The regex I have is working fine when I test it, so I don't think the problem is there. – Noam Gur Oct 31 '16 at 15:48