Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ <h1 class="title">Step 1</h1>
<li class="field">
<div class="control">
<input class="input" placeholder="{{ directive.placeholder }}" type="{{ directive.input }}"
{% if directive.tags contains 'Required' %} required {% endif %}
{% if directive.id == 'contact' %} oninput='checkContactValidity(this);' {% endif %}
{% if directive.tags contains 'Required' %} required {% endif %}
{% if directive.id == 'contact' %}
onchange='autoprefixEmail(this);'
{% endif %}
>
{% if directive.id == 'contact' %}
<p class="is-hidden help is-danger">
Expand Down
15 changes: 14 additions & 1 deletion js/genform.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function addAlternative(button) {
newInputControl.appendChild(newInput)

if (button.parentElement.parentElement.parentElement.id === 'contact') {
newInput.addEventListener('input', function () { checkContactValidity(newInput); });
newInput.addEventListener('change', function () { autoprefixEmail(newInput); });

var errorMessage = document.createElement('P');
newInputControl.appendChild(errorMessage);
Expand Down Expand Up @@ -180,3 +180,16 @@ function checkContactValidity(el) {
el.classList.add('is-danger');
}
}

function autoprefixEmail(inputEl) {
// Add mailto: prefix, e.g. contact@example.com -> mailto:contact@example.com

// NOTE: If this regular expression is changed in future, it should always fail if
// the user enters some other schema (e.g. https://) so they always have the option of overriding it.
// Currently this is achieved by not allowing a colon to appear in the input.
if (/^[a-z0-9_+.-]+@[a-z0-9_+.-]+$/i.test(inputEl.value)) {
inputEl.value = 'mailto:' + inputEl.value;
}

checkContactValidity(inputEl); // potentially remove validation error if issue resolved
}