<p>To conditionally add <code>http://</code> to strings that don't begin with <code>http://</code> or <code>https://</code>, use the Twig <a href="http://twig.sensiolabs.org/doc/templates.html#comparisons" rel="nofollow">matches operator</a> in a comparison.</p>

<p>To keep the syntax compact, I use a <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators" rel="nofollow">ternary operator</a> within the <code>set</code> tag. </p>

<pre><code>{# Set an example URL without `http://` #}
{% set url = 'example.com' %}

{# "Correct" the URL notation #}
{% set url = url matches '/^(https?:\\/\\/)/' ? url : 'http://' ~ url %}

{# Test output #}
{{ url }}
</code></pre>

<p>.</p>

<p>This is no full URL check in my regular expression, but you could simply work that in too. Pay attention to the escaping character <code>\</code> that (<a href="https://github.com/fabpot/Twig/pull/1457" rel="nofollow">currently</a>) needs to be escaped itself, so you end up with <code>\\</code> (it's wrong in the <a href="http://twig.sensiolabs.org/doc/templates.html#comparisons" rel="nofollow">Twig docs</a>).</p>

CC BY-SA 3.0
2014-08-26T15:37:27.820Z
Thank you for your answer, but unfortunatly it's not the answer i'm looking for. The input of a formfield should be changed on post, not on pageload.  Normally the [defaultSheme](http://www.yiiframework.com/doc/api/1.1/CUrlValidator#defaultScheme-detail) should automaticly prepend it, but for some reason this isn't working
Steven Vandemoortele
CC BY-SA 3.0
2014-08-26T14:48:48.567Z
2014-08-26T15:42:33.793Z
2014-08-26T15:42:33.793Z
carlcs
<p>Is is possible to use Craft's URL validation without <code>http://</code> or <code>https://</code> in the URL?
Or use some sort of function / filter to conditionally add it to the URL if it is missing?</p>

<p>I'm posting a form using sprout forms. The form contains a field that is set to validate as url.<br>
If you post this field without adding <code>http://</code> or <code>https://</code> it doesn't validate.<br>
Is it possible to somehow to set the <a href="http://www.yiiframework.com/doc/api/1.1/CUrlValidator#defaultScheme-detail" rel="nofollow"><code>defaultScheme</code></a> of an url field to http without changing the core Craft UrlValidator or Yii's CUrlValidator?</p>

CC BY-SA 3.0
2014-08-26T14:42:40.017Z
Do I get this right, you have a string like `example.com` that represents a URL but is without the `http://` part?
carlcs
CC BY-SA 3.0
2014-08-26T15:04:23.953Z
I'm posting a form which has a website field. In sprout forms i've defined the validation of that form field to be url. However when i post the form, it requires the url to have http:// or https:// in it.

I want users to be able to post example.com and either have it validated as being an url or automaticly have http:// prepended to it
Steven Vandemoortele
<templating><url><conditional><plugin-sproutforms>
URL validation without http:// or https:// in the URL
# URL validation without http:// or https:// in the URL

(Asked by: username_0 on 2014-08-26)

Is is possible to use Craft's URL validation without `http://` or `https://` in the URL?
Or use some sort of function / filter to conditionally add it to the URL if it is missing?

I'm posting a form using sprout forms. The form contains a field that is set to validate as url.  
If you post this field without adding `http://` or `https://` it doesn't validate.  
Is it possible to somehow to set the [`defaultScheme`](http://www.yiiframework.com/doc/api/1.1/CUrlValidator#defaultScheme-detail) of an url field to http without changing the core Craft UrlValidator or Yii's CUrlValidator?

**Tags:** templating, url, conditional, plugin-sproutforms

> **Comments:**
> **username_1**: Do I get this right, you have a string like `example.com` that represents a URL but is without the `http://` part?  
> **username_0**: I'm posting a form which has a website field. In sprout forms i've defined the validation of that form field to be url. However when i post the form, it requires the url to have http:// or https:// in it.  I want users to be able to post example.com and either have it validated as being an url or automaticly have http:// prepended to it  

---

## 1 answers

### Answer by username_1 on 2014-08-26. Score: 3

To conditionally add http:// to strings that don't begin with http:// or https://, use the Twig matches operator in a comparison.
To keep the syntax compact, I use a ternary operator within the set tag. 
{# Set an example URL without `http://` #}
{% set url = 'example.com' %}

{# "Correct" the URL notation #}
{% set url = url matches '/^(https?:\\/\\/)/' ? url : 'http://' ~ url %}

{# Test output #}
{{ url }}

.
This is no full URL check in my regular expression, but you could simply work that in too. Pay attention to the escaping character \ that (currently) needs to be escaped itself, so you end up with \\ (it's wrong in the Twig docs).

> **Comments:**
> **username_0**: Thank you for your answer, but unfortunatly it's not the answer i'm looking for. The input of a formfield should be changed on post, not on pageload.  Normally the [defaultSheme](http://www.yiiframework.com/doc/api/1.1/CUrlValidator#defaultScheme-detail) should automaticly prepend it, but for some reason this isn't working  

---

