I have designed a a small frontend project, a sign-up page. It uses HTML, CSS, JavaScript and jQuery. Being a beginner to frontend development, I want to adapt to all the best practices. I am looking forward to all your feedback.
My website has the following features:
- View the optimal layout for the site depending on their device's screen size
- See hover states for all interactive elements on the page
- Receive an error message when the
form
is submitted if:- Any
input
field is empty. - The email address is not formatted correctly (i.e. a correct email address should have this structure:
[email protected]
).
- Any
CODE :
function isEmail(email) {
var regex = /^([a-zA-Z0-9_.+-])+\@@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
$("button").on("click", function(){
if (!$(".box1").val()) {
$(".box1").val("🚫 Name cannot be empty").show();
$(".box1").css("color", "red");
}
if (!$(".box2").val()) {
$(".box2").val("🚫 Name cannot be empty").show();
$(".box2").css("color", "red");
}
if (!$(".box3").val()) {
$(".box3").val("🚫 Email cannot be empty").show();
$(".box3").css("color", "red");
}
var email = $(".box3").val();
if (!isEmail(email)){
$(".box3").val("🚫 Looks like this is not an email").show();
$(".box3").css("color", "red");
}
if (!$(".box4").val()) {
$(".box4").val("🚫 Password cannot be empty").show();
$(".box4").css("color", "red");
$(".box4").get(0).type = 'text';
}
})
body {
background-image: url(images/bg-intro-desktop.png);
background-color: hsl(0, 100%, 74%);
font-family: "Poppins";
}
h1 {
color: white;
font-size: 45px;
line-height: 50px;
}
p {
color: white;
font-family: "Poppins";
}
footer {
bottom: 0%;
width: 100%;
margin-top: 10px;
}
.attribution {
font-size: 11px;
text-align: center;
}
.attribution a {
color: hsl(228, 45%, 44%);
}
.parent {
margin-top: 8%;
display: flex;
}
.left-div {
padding: 5% 0% 0% 10%;
width: 35%;
text-align: left;
flex: auto;
}
.right-div {
flex: auto;
width: 50%;
}
.offer-div {
color: white;
background-color: hsl(248, 32%, 49%);
text-align: center;
margin: auto;
margin-bottom: 20px;
margin-left: 10%;
padding: 2%;
width: 55%;
box-shadow: 5px 5px rgba(0, 0, 0, 0.5);
border-radius: 4%;
}
.signup-box {
background-color: white;
max-width: 55%;
height: 75%;
box-shadow: 5px 5px rgba(0, 0, 0, 0.5);
border-radius: 4%;
margin: auto;
margin-left: 10%;
padding: 3%;
}
.input-box {
display: block;
width: 90%;
padding: 3%;
margin: auto;
margin-bottom: 20px;
border-radius: 3%;
border-color: rgb(226 211 211);
font-family: "Poppins";
font-weight: bold;
}
input[type=text]:focus {
outline: 1px solid hsl(248, 32%, 49%);
}
.btn {
background-color: hsl(154, 59%, 51%);
color: white;
display: block;
width: 97%;
padding: 3%;
border: none;
border-radius: 3%;
margin: auto;
margin-top: 20px;
margin-bottom: 20px;
font-family: "Poppins";
}
.btn:hover{
background-color: blue;
cursor: pointer;
}
#terms {
font-size: 10px;
text-align: center;
color: gray;
}
@media only screen and (max-width: 500px){
body{
background-image: url(./images/bg-intro-mobile.png);
}
h1{
font-size: 40px;
line-height: 40px;
}
.parent {
margin-top: 50px;
display: block;
}
.left-div {
display: block;
padding: 0%;
margin: auto;
text-align: justify;
width: 80%;
}
.offer-div {
margin: auto;
margin-bottom: 20px;
margin-left: 25px;
padding: 2%;
width: 75%;
}
.signup-box {
max-width: 75%;
height: 75%;
margin: auto;
margin-left: 25px;
padding: 3%;
}
.right-div {
display: block;
margin: auto;
margin-left: 5%;
width: 100%;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- displays site properly based on user's device -->
<link rel="icon" type="image/png" sizes="32x32" href="./images/favicon-32x32.png">
<title>Frontend Mentor | Sign Up</title>
<!--CSS Links-->
<link rel="stylesheet" href="styles.css">
<!--Google Fonts-->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
</head>
<body>
<main>
<div class="parent">
<div class="left-div">
<h1> Learn to code by watching others </h1>
<p> See how experienced developers solve problems in real-time. Watching scripted tutorials is great, but understanding how developers think is invaluable.</p>
</div>
<div class="right-div">
<div class="offer-div">
<b>Try it free 7 days </b> then $20/mo. thereafter
</div>
<div class="signup-box">
<input class="input-box box1" type="text" placeholder="First Name">
<input class="input-box box2" type="text" placeholder="Last Name">
<input class="input-box box3" type="text" placeholder="Email Address">
<input class="input-box box4" type="password" placeholder="Password">
<button class="btn">Claim your free trial</button>
<p id="terms">By clicking the button, you are agreeing to our <span style="color: hsl(0, 100%, 74%) ;">Terms and Services</span></p>
</div>
</div>
</div>
</main>
<footer>
<p class="attribution">
Challenge by <a href="https://www.frontendmentor.io/profile/chayansurana3" target="_blank">Frontend Mentor</a>.
Coded by <a href="https://github.com/chayansurana3" target="_blank">Chayan Surana</a>.
</p>
</footer>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="./index.js"></script>
</body>
</html>
type="email" required
to enforce the user typing a valid email address. The regex is no longer needed in modern browsers. The same goes forjQuery
. Unless you really need your code to run in Internet Explorer 8 for some kind of intranet that cannot upgrade their browsers, you can replace$
withdocument.querySelector()
. \$\endgroup\$