From 060056e793fb23bc5a8f03fb243f6ae3a8de53db Mon Sep 17 00:00:00 2001 From: Robert McGovern Date: Thu, 20 Oct 2022 00:01:07 +0100 Subject: [PATCH] hack that puts button and error message beside input field, but possibly means cannot use button --- .../base-apparel-coming-soon/css/style.css | 34 ++++++--- .../base-apparel-coming-soon/index.html | 12 +-- .../base-apparel-coming-soon/js/script.js | 75 ++++++++++++++++++- 3 files changed, 105 insertions(+), 16 deletions(-) diff --git a/projects/FrontendMentor/newbie/base-apparel-coming-soon/css/style.css b/projects/FrontendMentor/newbie/base-apparel-coming-soon/css/style.css index 1538b7d..10e516f 100644 --- a/projects/FrontendMentor/newbie/base-apparel-coming-soon/css/style.css +++ b/projects/FrontendMentor/newbie/base-apparel-coming-soon/css/style.css @@ -134,19 +134,25 @@ main { } .email-form { + margin: 0 auto; margin-top: 2rem; - position: relative; + /* position: relative; */ + display: flex; + justify-content: space-between; + border: 1px solid var(--col-field); + text-align: center; + width: 31ch; + border-radius: var(--border-radius); } .email-form__input { - border: 1px solid var(--col-field); - width: 40ch; - + /* width: 30ch; */ font-size: var(--fs-field); line-height: var(--lh-field); font-weight: var(--fw-field); padding: 0.625rem 0 0.625rem var(--adjustment-left-field-and-error); - border-radius: var(--border-radius); + border: none; background: inherit; + width: 100%; color: var(--col-neutral-dark-grayish-red); } @@ -160,13 +166,14 @@ main { } .email-form__button-submit { - position: absolute; + /* position: absolute; */ padding: 0.8125rem 1.6938rem 0.8125rem 1.75rem; border-radius: var(--border-radius); box-shadow: 0px 0.9375rem 1.25rem rgba(198, 110, 110, 0.247569); background: var(--gradient-pink); - right: 0; + /* right: 0; */ border: none; + margin-left: 0.5rem; cursor: pointer; } @@ -185,6 +192,13 @@ main { margin-top: 0.5rem; } +.email-form__error-icon { + opacity: 0; + height: 100%; + align-self: center; + /* width: */ +} + .error-hidden { opacity: 0; } @@ -275,17 +289,19 @@ main { } .email-form { + margin: unset; margin-top: 2.5rem; + width: 45ch; } .email-form__input { - width: 45ch; + /* width: 80ch; */ padding: 0.9375rem 0 0.9375rem var(--adjustment-left-field-and-error); } .email-form__button-submit { padding: 1.125rem 2.8188rem 1.125rem 2.875rem; - right: 6.875rem; + margin-left: 1rem; } .email-form__error-message { diff --git a/projects/FrontendMentor/newbie/base-apparel-coming-soon/index.html b/projects/FrontendMentor/newbie/base-apparel-coming-soon/index.html index 9f2cc90..04294a8 100644 --- a/projects/FrontendMentor/newbie/base-apparel-coming-soon/index.html +++ b/projects/FrontendMentor/newbie/base-apparel-coming-soon/index.html @@ -51,13 +51,15 @@ Hello fellow shoppers! We're currently building our new fashion store. Add your email below to stay up-to-date with announcements and our launch deals.

-
+
+

Please provide a valid email

diff --git a/projects/FrontendMentor/newbie/base-apparel-coming-soon/js/script.js b/projects/FrontendMentor/newbie/base-apparel-coming-soon/js/script.js index 3414a5f..47b1acf 100644 --- a/projects/FrontendMentor/newbie/base-apparel-coming-soon/js/script.js +++ b/projects/FrontendMentor/newbie/base-apparel-coming-soon/js/script.js @@ -1,26 +1,67 @@ "uses strict"; +/* + * this is my "simple" regex for email addresses. It does not cover all the possibilities that are allowed + * for the local and domain parts. For a more "correct" / comprehensive regex, there is one at + * https://stackoverflow.com/questions/3844431/are-email-addresses-allowed-to-contain-non-alphanumeric-characters + * + * For local part you can use ASCII: + * Latin letters A - Z a - z + * digits 0 - 9 + * special characters !#$%&'*+-/=?^_`{|}~ + * dot ., that it is not first or last, and not in sequence + * space and "(),:;<>@[] characters are allowed with restrictions (they are only allowed inside a quoted string, a backslash or double-quote must be preceded by a backslash) + * Plus since 2012 you can use international characters above U+007F, encoded as UTF-8. + * + * Domain part is more restricted: + * Latin letters A - Z a - z + * digits 0 - 9 + * hyphen -, that is not first or last, multiple hyphens in sequence are allowed. + * + * Recommended regex is: ^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,}) + */ +const validEmailFormat = /^[A-Za-z0-9][\w\+]*@[A-Za-z0-9]{3,}\.[A-Za-z]{2,}$/i; + const emailForm = document.getElementById("email-form"); const emailInput = document.getElementById("email"); +const submitButton = document.getElementById("submit-button"); const errorMessage = document.getElementById("errorMessage"); +const errorIcon = document.getElementById("error-icon"); -emailForm.addEventListener("submit", checkEmailValidity, false); +submitButton.addEventListener("click", checkEmailValidity, false); emailInput.addEventListener("focus", inputFocused, false); /** * @param {[Event]} event */ function checkEmailValidity(event) { - if (emailForm.checkValidity() === false) { + if (validateEmailAddress() === false) { event.preventDefault(); emailInput.classList.add("error-border"); errorMessage.classList.add("error-visible"); + errorIcon.classList.add("error-visible"); } else { emailInput.classList.remove("error-border"); errorMessage.classList.remove("error-visible"); } } +/* + */ +function validateEmailAddress() { + const submittedEmailAddress = emailInput.value; + // test email is valid via regex + const isValidEmailAddress = validEmailFormat.test(submittedEmailAddress); + + if (submittedEmailAddress.length === 0) { + return false; + } else if (!isValidEmailAddress) { + return false; + } else { + return true; + } +} + // When an input has focus I remove the error state, function inputFocused(event) { /** @@ -32,5 +73,35 @@ function inputFocused(event) { if (input.classList.contains("error-border")) { input.classList.remove("error-border"); errorMessage.classList.remove("error-visible"); + errorIcon.classList.remove("error-visible"); } } + +function testRegex() { + console.log("Should all be false"); + console.log(validEmailFormat.test("submittedEmailAddress")); + console.log(validEmailFormat.test("s@.NET")); + console.log(validEmailFormat.test("@")); + console.log(validEmailFormat.test("@.")); + console.log(validEmailFormat.test("@.net")); + console.log(validEmailFormat.test("@tarasis.net")); + console.log(validEmailFormat.test("adsadasds@")); + console.log(validEmailFormat.test("__submittedEmailAddress")); + console.log(validEmailFormat.test("spam@tarasis.99")); + console.log(validEmailFormat.test("rob@.net")); + console.log(validEmailFormat.test("spam@tarasis")); + console.log(validEmailFormat.test("spam@tarasis.a")); + console.log(validEmailFormat.test("s@ta.net")); + console.log(validEmailFormat.test("_@tarasis.net")); + console.log(validEmailFormat.test("_3@tarasis.net")); + + console.log("\n Should all be true"); + console.log(validEmailFormat.test("spam@tarasis.net")); + console.log(validEmailFormat.test("rob@tad.io")); + console.log(validEmailFormat.test("s@tarasis.net")); + console.log(validEmailFormat.test("s@tarasis.net")); + console.log(validEmailFormat.test("s_@tarasis.net")); + console.log(validEmailFormat.test("s+blah@tarasis.net")); // this can be true, some servers use this for filtering +} + +// testRegex();