Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-26T15:37:27.820Z
Answers.Text: 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
Answers.UserDisplayName: Steven Vandemoortele
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-26T14:48:48.567Z
Answers.LastActivityDate: 2014-08-26T15:42:33.793Z
Answers.LastEditDate: 2014-08-26T15:42:33.793Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-08-26T14:42:40.017Z
Comments.Text: Do I get this right, you have a string like `example.com` that represents a URL but is without the `http://` part?
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-08-26T15:04:23.953Z
Comments.Text: 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
Comments.UserDisplayName: Steven Vandemoortele
Tags: <templating><url><conditional><plugin-sproutforms>
Title: URL validation without http:// or https:// in the URL
ThreadText: # 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](http://twig.sensiolabs.org/doc/templates.html#comparisons) in a comparison.
  
  To keep the syntax compact, I use a [ternary operator](http://twig.sensiolabs.org/doc/templates.html#other-operators) 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](https://github.com/fabpot/Twig/pull/1457)) needs to be escaped itself, so you end up with `\\` (it's wrong in the [Twig docs](http://twig.sensiolabs.org/doc/templates.html#comparisons)).
  
  > **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  
  
  ---
  
  