Skip to content

Commit ba49e36

Browse files
authored
Merge pull request #236 from plausible/domain_js_validation
Added: JS validation for domain_name in Settings screen and Configuration wizard.
2 parents ed174e8 + 22cdd11 commit ba49e36

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

assets/src/js/admin/main.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ document.addEventListener('DOMContentLoaded', () => {
142142
const options = [];
143143

144144
inputs.forEach(function (input) {
145+
input = plausible.validateInput(input);
146+
145147
options.push({name: input.name, value: input.value});
146148
});
147149

@@ -158,6 +160,21 @@ document.addEventListener('DOMContentLoaded', () => {
158160
plausible.ajax(form, button);
159161
},
160162

163+
/**
164+
* Currently only validates the domain_name input, but can be used in the future for other custom input validations.
165+
*
166+
* @param input
167+
* @returns {*}
168+
*/
169+
validateInput: function (input) {
170+
// Strip http(s)://(www.) from domain_name before sending it.
171+
if (input.name === 'domain_name' && input.value.match(/^(https?:\/\/)?(www.)?/).length > 0) {
172+
input.value = input.value.replace(/^(https?:\/\/)?(www.)?/, '');
173+
}
174+
175+
return input;
176+
},
177+
161178
/**
162179
* Save Options on Next click for API Token and Domain Name slides.
163180
*
@@ -175,6 +192,8 @@ document.addEventListener('DOMContentLoaded', () => {
175192
let options = [];
176193

177194
for (let input of inputs) {
195+
input = plausible.validateInput(input);
196+
178197
options.push({name: input.name, value: input.value});
179198
}
180199

0 commit comments

Comments
 (0)