hack that puts button and error message beside input field, but possibly means cannot use button

This commit is contained in:
Robert McGovern 2022-10-20 00:01:07 +01:00
parent aa9a4c5903
commit 060056e793
3 changed files with 105 additions and 16 deletions

View File

@ -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 {

View File

@ -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.
</p>
<form class="email-form" id="email-form" action="submit" novalidate>
<div class="email-form" id="email-form" action="submit" novalidate>
<label class="visually-hidden" for="email">Email</label>
<input class="email-form__input" placeholder="Email Address" type="email" name="email" id="email" required>
<button class="email-form__button-submit" type="submit"><img src="images/icon-arrow.svg" alt="Submit Button">
</button>
<p class="email-form__error-message error-hidden" id="errorMessage">Please provide a valid email</p>
</form>
<!-- <button class="email-form__button-submit" type="submit"><img src="images/icon-arrow.svg" alt="Submit Button"> -->
<!-- </button> -->
<img class="email-form__error-icon" id="error-icon" src="images/icon-error.svg" alt="Error icon">
<img id="submit-button" class="email-form__button-submit" src="images/icon-arrow.svg" alt="Submit Button">
</div>
<p class="email-form__error-message error-hidden" id="errorMessage">Please provide a valid email</p>
</section>
</main>

View File

@ -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();