0

I am validating url on my form through regex.

^(?:http(s)?://)?[\w.-]+(?:.[\w.-]+)+[\w-._~:/?#[]@!\$&'()*+,;=.]+$

It validates all URL for example:

  • https://www.example.com
  • http://www.example.com
  • www.example.com
  • example.com
  • http://blog.example.com
  • http://www.example.com/product
  • http://www.example.com/products?id=1&page=2
  • http://www.example.com#up
  • http://255.255.255.255
  • 255.255.255.255


  • However it also validates URL like
  • www.google
  • www.example
  • www.example.
  • www.google.
  • which are not accepted URL's

    I am not too efficient with regex. Please help what needs to be changed

    5
    • Try ^(?!www\.[^.]+$)(?:https?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w._~:/?#[\\\]@!$&'()*+,;=.-]+$ (demo). Commented Feb 19, 2019 at 11:23
    • Hi wiktor its working however it includes www.google. urls as well. Please can you help with that too... It should not include either www.google or www.google. Commented Feb 19, 2019 at 11:34
    • 1
      See regex101.com/r/mG9Qr5/2 Commented Feb 19, 2019 at 11:39
    • @WiktorStribiżew thanks it works!! Commented Feb 19, 2019 at 11:40
    • www.google is a valid domain name. The fact that the .google top level domain is not available on the public internet doesn't stop someone with access to their own name server from setting it up as a local domain name. I think you need to have a think about what you want to accept as a valid url. Commented Feb 19, 2019 at 11:45

    1 Answer 1

    1

    When using a regex in HTML5 pattern attribute you should escape characters very carefully, as those browsers that have ES6+ standard implemented might throw an exception when they "see" [\w\.-] (no need to escape dot, and once the pattern is compiled with u flag, it becomes an error).

    Now, to fix the issue, you may add a (?!www\.[^.]+\.?$) lookahead after ^ to fail all inputs that start with www. and then have any 0 or more chars other than . and then an optional . at the end of the string.

    You may use

    ^(?!www\.[^.]+\.?$)(?:https?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w._~:/?#[\\\]@!$&'()*+,;=.-]+$
    

    See the regex demo. Note I escaped both \ and ] in your pattern, I think you meant to match both (your original regex does not match \ with [\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]).

    Note that the HTML5 pattern regex is anchored by default, you need no ^ and $ at the start/end:

    pattern="(?!www\.[^.]+\.?$)(?:https?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w._~:/?#[\\\]@!$&'()*+,;=.-]+"
    

    But you may still keep them if you want.

    Sign up to request clarification or add additional context in comments.

    1 Comment

    I need a little help again, I have come across a situation, where example.com/ is verifying as well whereas it should not... example.com/product is correct however example.com/ is not... please help to overcome this situation as well

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.