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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
<html>
<head>
<title>Form Validation</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Google Fonts -->
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap"
rel="stylesheet"
/>
<!-- Stylesheet -->
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<form autocomplete="off">
<h4>Create Your Account</h4>
<div class="input-section">
<label>First Name <span class="required-color">*</span></label>
<input
type="text"
placeholder="Enter First Name"
id="first-name-input"
required
/>
<span id="first-name-error" class="hide required-color error-message"
>Invalid Input</span
>
<span id="empty-first-name" class="hide required-color error-message"
>First Name Cannot Be Empty</span
>
</div>
<div class="input-section">
<label>Last Name <span class="required-color">*</span></label>
<input
type="text "
placeholder="Enter Last Name"
id="last-name-input"
required
/>
<span id="last-name-error" class="hide required-color error-message"
>Invalid Input</span
>
<span id="empty-last-name" class="hide required-color error-message">
Last Name Cannot Be Empty
</span>
</div>
<div class="input-section">
<label>Email <span class="required-color">*</span></label>
<input type="email" placeholder="Enter Email" id="email" required />
<span id="email-error" class="hide required-color error-message"
>Invalid Email</span
>
<span id="empty-email" class="hide required-color error-message"
>Email Cannot Be Empty</span
>
</div>
<div class="input-section">
<label>Phone <span class="required-color">*</span></label>
<input
type="text"
placeholder="Enter Phone Number"
id="phone"
required
/>
<span id="phone-error" class="hide required-color error-message"
>Phone Number Should Have 10 Digits</span
>
<span id="empty-phone" class="hide required-color error-message">
Phone cannot be empty
</span>
</div>
<div class="input-section">
<label>Password <span class="required-color">*</span></label>
<input
type="password"
placeholder="Enter Password"
id="password"
required
/>
<span id="password-error" class="hide required-color error-message">
Passwords Should Have Letter, Special symbols, Numbers And Length >=
8
</span>
<span id="empty-password" class="hide required-color error-message">
Password Cannot Be Empty
</span>
</div>
<div class="input-section">
<label>Confirm Password <span class="required-color">*</span></label>
<input
type="password"
id="verify-password"
placeholder="Confirm Password"
required
/>
<span
id="verify-password-error"
class="hide required-color error-message"
>Should Be Same As Previous Password</span
>
<span
id="empty-verify-password"
class="hide required-color error-message"
>Password Cannot Be Empty</span
>
</div>
<button id="submit-button">Sign Up</button>
</form>
</div>
<!-- Script -->
<script src="script.js"></script>
</body>
</html>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
background-color: #3164ff;
}
.container {
width: 90vw;
max-width: 600px;
padding: 2em 1.5em;
background-color: #ffffff;
margin: 1em auto;
border-radius: 0.5em;
}
h4 {
font-size: 1.5em;
}
.input-section {
margin: 0.5em 0;
}
label,
.error-message {
display: block;
font-size: 1em;
}
label {
margin-bottom: 0.5em;
font-weight: 500;
}
.error-message {
margin-top: 0.2em;
}
input,
button {
display: block;
outline: none;
width: 100%;
padding: 0.5em;
border-radius: 0.3em;
}
input {
padding: 1em 0.5em;
border: 1.5px solid #d0d0d0;
}
button {
font-size: 1em;
background-color: #3164ff;
border: none;
color: #ffffff;
padding: 0.8em 0;
margin-top: 2em;
}
.required-color {
color: #ff4747;
}
.error {
border-color: #ff4747;
}
.valid {
border-color: #37a137;
}
.hide {
display: none;
}
@media only screen and (max-width: 450px) {
.container {
font-size: 14px;
}
}
//First Name
let firstNameInput = document.getElementById("first-name-input");
let firstNameError = document.getElementById("first-name-error");
let emptyFirstNameError = document.getElementById("empty-first-name");
//Last name
let lastNameInput = document.getElementById("last-name-input");
let lastNameError = document.getElementById("last-name-error");
let emptyLastNameError = document.getElementById("empty-last-name");
//Phone
let phoneInput = document.getElementById("phone");
let phoneError = document.getElementById("phone-error");
let emptyPhoneError = document.getElementById("empty-phone");
//Email
let emailInput = document.getElementById("email");
let emailError = document.getElementById("email-error");
let emptyEmailError = document.getElementById("empty-email");
//Password
let passwordInput = document.getElementById("password");
let passwordError = document.getElementById("password-error");
let emptyPasswordError = document.getElementById("empty-password");
//Verify Password
let verifyPasswordInput = document.getElementById("verify-password");
let verifyPasswordError = document.getElementById("verify-password-error");
let emptyVerifyPasswordError = document.getElementById("empty-verify-password");
//Submit
let submitButton = document.getElementById("submit-button");
//Valid
let validClasses = document.getElementsByClassName("valid");
let invalidClasses = document.getElementsByClassName("error");
//Password Verification
const passwordVerify = (password) => {
const regex =
/^(?=.+[a-z])(?=.+[A-Z])(?=.+[0-9])(?=.+[\$\%\^\&\!@\#\*\(\)\+\=`~\?\>\<])/;
return regex.test(password) && password.length >= 8;
};
//Text verification (if input contains only text)
const textVerify = (text) => {
const regex = /^[a-zA-Z]{3,}$/;
return regex.test(text);
};
//Phone number verification
const phoneVerify = (number) => {
const regex = /^[0-9]{10}$/;
return regex.test(number);
};
//Email verification
const emailVerify = (input) => {
const regex = /^[a-z0-9_]+@[a-z]{3,}\.[a-z\.]{3,}$/;
return regex.test(input);
};
//For empty input - accepts(input,empty error for that input and other errors)
const emptyUpdate = (
inputReference,
emptyErrorReference,
otherErrorReference
) => {
if (!inputReference.value) {
//input is null/empty
emptyErrorReference.classList.remove("hide");
otherErrorReference.classList.add("hide");
inputReference.classList.add("error");
} else {
//input has some content
emptyErrorReference.classList.add("hide");
}
};
//For error styling and displaying error message
const errorUpdate = (inputReference, errorReference) => {
errorReference.classList.remove("hide");
inputReference.classList.remove("valid");
inputReference.classList.add("error");
};
//For no errors
const validInput = (inputReference) => {
inputReference.classList.remove("error");
inputReference.classList.add("valid");
};
//First name
firstNameInput.addEventListener("input", () => {
if (textVerify(firstNameInput.value)) {
//If verification returns true
firstNameError.classList.add("hide");
validInput(firstNameInput);
} else {
//for false
errorUpdate(firstNameInput, firstNameError);
//empty checker
emptyUpdate(firstNameInput, emptyFirstNameError, firstNameError);
}
});
//Last name
lastNameInput.addEventListener("input", () => {
if (textVerify(lastNameInput.value)) {
lastNameError.classList.add("hide");
validInput(lastNameInput);
} else {
errorUpdate(lastNameInput, lastNameError);
emptyUpdate(lastNameInput, emptyLastNameError, lastNameError);
}
});
//Phone
phoneInput.addEventListener("input", () => {
if (phoneVerify(phoneInput.value)) {
phoneError.classList.add("hide");
validInput(phoneInput);
} else {
errorUpdate(phoneInput, phoneError);
emptyUpdate(phoneInput, emptyPhoneError, phoneError);
}
});
//Email
emailInput.addEventListener("input", () => {
if (emailVerify(emailInput.value)) {
emailError.classList.add("hide");
validInput(emailInput);
} else {
errorUpdate(emailInput, emailError);
emptyUpdate(emailInput, emptyEmailError, emailError);
}
});
//Password
passwordInput.addEventListener("input", () => {
if (passwordVerify(passwordInput.value)) {
passwordError.classList.add("hide");
validInput(passwordInput);
} else {
errorUpdate(passwordInput, passwordError);
emptyUpdate(passwordInput, emptyPasswordError, passwordError);
}
});
//Verify password
verifyPasswordInput.addEventListener("input", () => {
if (verifyPasswordInput.value === passwordInput.value) {
verifyPasswordError.classList.add("hide");
validInput(verifyPasswordInput);
} else {
errorUpdate(verifyPasswordInput, verifyPasswordError);
emptyUpdate(passwordInput, emptyVerifyPasswordError, verifyPasswordError);
}
});
//Submit button
submitButton.addEventListener("click", () => {
if (validClasses.length == 6 && invalidClasses.length == 0) {
alert("Success");
} else {
alert("Error");
}
});
Also see: Tab Triggers