https://bookmark-landing-page-psi-fawn.vercel.app/
If you go to the bottom of the page there is an email textbox with a submit button. When you submit an invalid email, it gives a custom message ‘Whoops, you entered an invalid email’ etc with custom styling.
I know about the :invalid selector but this isn’t that. How did this person accomplish this?
Right now I get the ‘Please enter a valid email’ message with a little white dialog box. This is the behavior I want to overwrite
(Put an invalid email in the below and hit the submit button, that popup is what I want to prevent and use my own custom div instead)
<form class="cta-form">
<input class="email-textbox" type="email" placeholder="Enter your email address">
<input class="btn-contact" type="submit" value="Contact Us">
</form>
2
Answers
Looking at the page’s Javascript, you can find the handler for the form submission on line 109. This code is what is applying those custom error displays. To prevent the browser’s validation from activating, they set the input to
type="text"
, rather thantype="email"
, but you should instead use thenovalidate
attribute on the form element, to preserve autofill and other browser functionality.Here’s a simple example that checks if the email includes an
@
character, and hides and shows error text accordingly.I think you should use JavaScript to solve this. Change the input type from email to text and use regex to validate the input. When submitting if the email doesn’t match the format then, show the dialog box, you wished to appear.
This requires a bit more work but this is how it goes.
Pseudocode:
-> take input in form of text
-> give that input an id and access it using JS
-> create a function that check for email using regex
-> run the function when the submit button is clicked
-> if email match the format then submit else show dialog box