Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Thomas Kahn 602 posts 506 karma points
    Oct 19, 2009 @ 15:47
    Thomas Kahn
    0

    Regexp for checking if the user has entered http:// or https:// in the beginning of a textstring?

    Hi!

    I'm trying to use validation on an input field in Umbraco to see if the user has entered http:// or https:// in the beginning of a textstring. The regexp I'm using looks like this:

    ^(http://|https://)

    It works if http:// or https:// is the only thing you put in the field, but if you enter a real URL (http://www.mydomain.com), it says it's not the correct format. Using a regexp tester, it seems like the syntax is correct though?

    Is it my regexp code that is not correct or is it how Umbraco interprets it?

    Thanks in advance!

    /Thomas Kahn

  • Josh Townson 67 posts 162 karma points
    Oct 20, 2009 @ 13:01
    Josh Townson
    0

    Hi Thomas,

    I would suggest always making sure the regex matches the whole input, and include the end character "$"

    That regex doesn't allow anything after the http or https. Depending on what you want to achieve, you will need to decide what other characters are going to be allowed. Try:

    ^(http|https)\://[a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~]+$

    This allows 1 or more letters, numbers and symbols that are allowed in a URL (not tested - but it looks right)

    The following should allow absolutely anything after the http:// or https:// (or even nothing) except for newline characters

    ^(http|https)\://[.]*$

     

  • Thomas Kahn 602 posts 506 karma points
    Oct 21, 2009 @ 09:39
    Thomas Kahn
    0

    Hi Josh!

    When I tried using this regex:

    ^(http|https)\://[.]*$

    ...with the link http://www.test.com, Umbraco says it's not a correct format. This one works though:

    ^(http|https)\://[a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~]+$

    Thanks for the reply!

    /Thomas

  • Josh Townson 67 posts 162 karma points
    Oct 21, 2009 @ 13:37
    Josh Townson
    1

    Hi Thomas - it seems I was having some sort of brain fart - ther is no need of the square brackets round the "." in the basic regex - but it is a pretty daft regex anyway - as all it does is enforce that the string starts with http(s):// - amd allows spaces and pretty much all other non-uri compliant characters. If you relly want such a regex this is it:

    ^(http|https)\://.*$
  • Thomas Kahn 602 posts 506 karma points
    Oct 23, 2009 @ 16:54
    Thomas Kahn
    0

    Hi Josh!

    I just want to prevent administrators from entering incomplete links like "www.test.com" - omitting the http or https prefix. Since browsers can handle links that don't start with "http" users often assume that it's OK to omit it in link fields in CMS's as well. Otherwise I trust that they get the rest right. :-)

    Thanks again for sorting this out!

    /Thomas

  • Brian Juul Andersen 43 posts 97 karma points c-trib
    Aug 15, 2012 @ 15:32
    Brian Juul Andersen
    0

    Thank you guys. Just what I needed.

Please Sign in or register to post replies

Write your reply to:

Draft