X-Git-Url: https://git.openstreetmap.org./nominatim-ui.git/blobdiff_plain/a6e3425fd49331782c847b18668ad12ee5984d08..b3f82d79148184cdb999f9a64f20fce34c3b8dad:/src/components/UrlSubmitForm.svelte diff --git a/src/components/UrlSubmitForm.svelte b/src/components/UrlSubmitForm.svelte index 019fa8c..975e79f 100644 --- a/src/components/UrlSubmitForm.svelte +++ b/src/components/UrlSubmitForm.svelte @@ -17,8 +17,32 @@ return params; } + + // https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation + // doesn't support hidden fields, so we check those in an extra step + function validate_field(field) { + if (field.type === 'hidden') { + if (field.pattern && !field.value.match(field.pattern)) return false; + } + return field.checkValidity(); // for hidden field always true + } + + function handle_submit(event) { + let form = event.target; + + let allow_submit = true; + + Array.prototype.slice.call(form.elements).forEach(function (field) { + if (!validate_field(field)) { + alert('Invalid input in ' + field.name); + allow_submit = false; + } + }); + + if (allow_submit) refresh_page(page, serialize_form(form)); + } -