font-weight: bold;
}
+ /* Display form errors for the one browser that doesn't support
+ * them natively, mobile Firefox. We use "visibility" to toggle
+ * it on and off, but "display" to hide it completely in non-
+ * stupid web browsers. */
+ #marc-form-errors {
+ color: #a00;
+ visibility: hidden;
+ display: none;
+ }
+
svg {
/* Set the height to 100% of the screen, which we'll keep; and the
* initial position to (0,0), which we're going to change
<label for="destination8">West Baltimore (Penn Line)</label>
</div>
+ <p id="marc-form-errors">OK</p>
+
<input type="hidden" name="serviceid" value="R" />
<input type="hidden" name="servicename" value="MARC Train" />
- <input type="submit" name="go" value="Generate Ticket" />
+ <input type="submit"
+ name="go"
+ id="marc-submit"
+ value="Generate Ticket" />
</fieldset>
</form>
</div>
menu.style.display = "none";
}
+ /**
+ * Determine if the user agent is mobile Firefox.
+ */
+ function ua_is_mobile_ff() {
+ const ua = navigator.userAgent.toLowerCase();
+ return (ua.includes("firefox") && ua.includes("mobile"));
+ }
+
/**
* Validate the MARC form's origin/destination.
*
* zone information for the (origin,destination) pair, that indicates
* that it's probably not a valid choice; otherwise I would have
* filled in the information from the CharmPass app already.
+ *
+ * All browsers except mobile Firefox let us call setCustomValidity()
+ * to provide an error message that is displayed if the user tries
+ * to submit invalid choices. But amazingly, mobile Firefox does not:
+ *
+ * https://bugzilla.mozilla.org/show_bug.cgi?id=1510450
+ *
+ * Instead we have to work around it (in that one browser) by
+ * showing/hiding a paragraph that we fill with the errors.
*/
function validate_origin_destination(event) {
const origins = document.getElementsByName("origin");
const destinations = document.getElementsByName("destination");
+ const mfe = document.getElementById("marc-form-errors");
+ const marcsubmit = document.getElementById("marc-submit");
+
+ if (ua_is_mobile_ff()) {
+ /* Even though this is only for one browser, empty paragraphs
+ * are handled inconsistently and should be avoided as a rule.
+ * So, we make it say "OK" before we hide it. */
+ mfe.textContent = "OK";
+ mfe.style.visibility = "hidden";
+ marcsubmit.disabled = false;
+ }
let src = null;
let dest = null;
})
if (src.value === dest.value) {
- let err = "Origin and destination are the same!";
+ let err = "Origin and destination are the same";
dest.setCustomValidity(err);
+
+ if (ua_is_mobile_ff()) {
+ mfe.textContent = err;
+ mfe.style.visibility = "visible";
+ marcsubmit.disabled = true;
+ }
}
else if (compute_marc_zone(src.value, dest.value) === null) {
- let err = "Origin and destination are on different lines!";
+ let err = "Origin and destination are on different lines";
dest.setCustomValidity(err);
+
+ if (ua_is_mobile_ff()) {
+ mfe.textContent = err;
+ mfe.style.visibility = "visible";
+ marcsubmit.disabled = true;
+ }
}
}
(x) => x.addEventListener("change", validate_origin_destination)
);
- /* Also do it when the page loads, because firefox likes to
- * remember your selection even after the page reloads. */
+ /* Also do it when the page loads, because firefox likes to
+ * remember your selection even after the page reloads. */
window.addEventListener("load", validate_origin_destination);
+
+ /* Finally, we have to babysit mobile Firefox, who doesn't
+ * support HTML5 form validation going into 2024. Turn on
+ * the little form errors paragraph so we can toggle its
+ * visibility (and make it display the error) when the user
+ * makes an invalid selection. */
+ window.addEventListener("load", () => {
+ if (ua_is_mobile_ff()) {
+ const mfe = document.getElementById("marc-form-errors");
+ mfe.style.display = "block";
+ }
+ });
}
</script>