HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="./style.css">
</head>
<body>
<main>
<div class="container">
<div class="header">
<h2>Create an Account</h2>
</div>
<form class="form" id="form">
<div class="form-control">
<label for="username">Username</label>
<input type="text" id="username" placeholder="Enter your Username">
<i class="fa fa-smile-o"></i>
<i class="fa fa-frown-o"></i>
<p class="errorMessage">Error Message</p>
</div>
<div class="form-control">
<label for="email">Email</label>
<input type="email" id="email" placeholder="Enter your Email">
<i class="fa fa-smile-o"></i>
<i class="fa fa-frown-o"></i>
<p class="errorMessage">Error Message</p>
</div>
<div class="form-control">
<label for="password">Password</label>
<input type="password" id="password" placeholder="Enter your Password">
<i class="fa fa-smile-o"></i>
<i class="fa fa-frown-o"></i>
<p class="errorMessage">Error Message</p>
</div>
<div class="form-control">
<label for="password2">Confirm Password</label>
<input type="password" id="password2" placeholder="Confirm your Password">
<i class="fa fa-smile-o"></i>
<i class="fa fa-frown-o"></i>
<p class="errorMessage">Error Message</p>
</div>
<button>Submit</button>
</form>
<div class="complete-modal">
<h2>Successful!</h2>
</div>
</div>
</main>
<div class="animate-circles">
<div class="red circle"></div>
<div class="blue circle"></div>
<div class="red circle"></div>
<div class="circle"></div>
<div class="orange circle"></div>
<div class="red circle"></div>
<div class="circle"></div>
<div class="red circle"></div>
<div class="blue circle"></div>
<div class="orange circle"></div>
</div>
<script src="./app.js"></script>
</body>
</html>
* {
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
background-color: blueviolet;
}
h2,
p {
margin: 0;
}
main {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 40px 20px;
border-radius: 5px;
width: 400px;
max-width: 100%;
box-shadow: 0 0 10px 1px rgba(61, 61, 61, 0.19), 0 0 15px 1px rgba(63, 63, 63, 0.23);
}
.container.complete {
visibility: hidden;
}
.container .header {
padding-bottom: 20px;
}
h2 {
font-size: 20px;
}
.form-control {
margin-bottom: 10px;
position: relative;
}
.form-control label {
font-weight: bold;
font-size: 13px;
}
.form-control input {
margin-top: 2px;
display: block;
width: 100%;
padding: 10px;
border-radius: 4px;
border: 2px solid blueviolet;
font-family: inherit;
}
.form-control i {
position: absolute;
top: 29px;
right: 10px;
font-size: 24px;
visibility: hidden;
}
.form-control input:focus {
outline: none;
}
.form-control.success input {
border: 2px solid green;
}
.form-control.success .fa-smile-o {
color: green;
visibility: visible;
}
/* Animation code to move form fields */
@keyframes inputMove {
0% {
transform: translateX(5px);
}
25% {
transform: translateX(-5px);
}
50% {
transform: translateX(5px);
}
75% {
transform: translateX(-5px);
}
100% {
transform: translateX(0px);
}
}
/* Adding the animation properties to the error CSS selector */
.form-control.error input {
border: 2px solid red;
animation-name: inputMove;
animation-duration: .5s;
/* animation-iteration-count: 3; */
}
.form-control.error .fa-frown-o {
color: red;
visibility: visible;
}
.container.complete .fa {
visibility: hidden;
}
/* .container.error {
animation-name: inputMove;
animation-duration: .5s;
} */
.form-control .errorMessage {
visibility: hidden;
margin: 2px 0 12px 0;
font-weight: bold;
font-size: 11px;
}
.form-control.error .errorMessage {
visibility: visible;
color: red;
}
button {
font-family: inherit;
width: 100%;
padding: 10px;
background-color: blueviolet;
color: #fff;
font-size: 15px;
border-radius: 4px;
border: 2px solid blueviolet;
cursor: pointer;
transition: all .5s ease-in-out;
}
button:focus {
outline: none;
}
button:hover {
color: blueviolet;
border: 2px solid blueviolet;
background: transparent;
}
.complete-modal {
visibility: hidden;
position: fixed;
top: 45%;
left: 41%;
background-color: #fff;
padding: 40px 20px;
border-radius: 5px;
width: 300px;
max-width: 100%;
box-shadow: 0 0 10px 1px rgba(61, 61, 61, 0.19), 0 0 15px 1px rgba(63, 63, 63, 0.23);
color: green;
text-align: center;
}
.container.complete .complete-modal {
visibility: visible;
}
/* Animation code to move bubbles up */
@keyframes animCircle {
0% {
transform: translateY(0);
opacity: 0;
}
50% {
opacity: 1;
}
75% {
opacity: 1;
}
100% {
transform: translateY(-100vh);
opacity: 0;
}
}
.animate-circles {
width: 100%;
display: flex;
justify-content: space-around;
position: absolute;
bottom: -10vh;
z-index: -1;
visibility: hidden;
}
.animate-circles.complete {
visibility: visible;
}
.animate-circles div {
animation: animCircle 7s infinite linear;
}
.animate-circles div:nth-child(1) {
animation-delay: 5s;
}
.animate-circles div:nth-child(3) {
animation-delay: 2s;
}
.animate-circles div:nth-child(7) {
animation-delay: 6s;
}
.animate-circles div:nth-child(5) {
animation-delay: 4s;
}
.animate-circles div:nth-child(9) {
animation-delay: 4s;
}
.animate-circles div:nth-child(8) {
animation-delay: 2s;
}
.animate-circles .circle {
background-color: lawngreen;
height: 40px;
width: 40px;
border-radius: 50%;
}
.animate-circles .red {
background-color: red;
height: 30px;
width: 30px;
}
.animate-circles .blue {
background-color: blue;
height: 20px;
width: 20px;
}
.animate-circles .orange {
background-color: orange;
height: 40px;
width: 40px;
}
//Assigning DOM elements to variables
const form = document.getElementById('form');
const username = document.getElementById('username');
const email = document.getElementById('email');
const password = document.getElementById('password');
const password2 = document.getElementById('password2');
const container = document.querySelector('.container');
const animateCircles = document.querySelector('.animate-circles');
//Listen for for submission
form.addEventListener('submit', (e) => {
//prevent default loading when form is submitted
e.preventDefault();
// Get values of form fields and assign to new variables
const usernameValue = username.value;
const emailValue = email.value;
const passwordValue = password.value;
const password2Value = password2.value;
//conditional statements to check if form value is valid ..... If form value is not valid an error function is triggered but if it is valid a success function is triggered
if (usernameValue === '') {
errorMessage(username, "Username is empty");
} else {
successMessage(username);
}
if (emailValue === '') {
errorMessage(email, "Email is empty");
} else if (!validateEmail(emailValue)) {
errorMessage(email, "Email is invalid");
} else {
successMessage(email);
}
if (passwordValue === '') {
errorMessage(password, "Password is empty");
} else {
successMessage(password);
}
if (password2Value === '') {
errorMessage(password2, "Password is empty");
} else if (password2Value !== passwordValue) {
errorMessage(password2, "Both Passwords does not match");
} else {
successMessage(password2);
}
// conditional statement to check if all values are valid so the bubbles can appear
if (username.parentElement.classList.contains('success') && email.parentElement.classList.contains('success') && password.parentElement.classList.contains('success') && password2.parentElement.classList.contains('success')) {
container.classList.add('complete');
animateCircles.classList.add('complete');
}
});
// function to be triggered if form valu is not valid. This function simply adds the error CSS class and removes that of success if it exists
function errorMessage(value, message) {
const formControl = value.parentElement;
if (formControl.classList.contains('success')) {
formControl.classList.remove('success');
formControl.classList.add('error');
} else {
formControl.classList.add('error');
}
formControl.querySelector('.errorMessage').textContent = message;
}
// function to be triggered if form valu is valid. This function simply adds the success CSS class and removes that of error if it exists
function successMessage(value) {
const formControl = value.parentElement;
if (formControl.classList.contains('error')) {
formControl.classList.remove('error');
formControl.classList.add('success');
} else {
formControl.classList.add('success');
}
}
//This is a simple function to validate the email
function validateEmail(email) {
const re = /^(([^<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
Also see: Tab Triggers