Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created April 4, 2023 04:17
Show Gist options
  • Save prof3ssorSt3v3/9053d25e160cdd930e81aa189289a492 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/9053d25e160cdd930e81aa189289a492 to your computer and use it in GitHub Desktop.
Code from video about HTML pattern attribute
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pattern Attribute</title>
<script src="./main.js" type="module"></script>
<link rel="stylesheet" href="./main.css" />
</head>
<body>
<header>
<h1>HTML Input Pattern Attribute</h1>
</header>
<main>
<form action="#" method="get" id="sampleForm">
<p class="formBox">
<label for="fullname">Fullname</label>
<input name="fullname" id="fullname" type="text" title="both first and last name with a space between" required pattern="(^[a-zA-Z]{1,30} [a-zA-Z]{1,30}$)" />
</p>
<p class="formBox">
<label for="zipcode">Zipcode</label>
<input name="zipcode" id="zipcode" type="text" title="5 digit, or 9 digit, a 5 " required pattern="(^\d{5}$)|(^\d{9}$)|(^\d{5}-\d{4}$)" />
</p>
<p class="formBox">
<label for="postal">Postal Code</label>
<input
name="postal"
id="postal"
type="text"
title="letter digit letter digit letter digit"
required
pattern="(^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ]\s?\d[ABCEGHJKLMNPRSTVWXYZ]\d$)"
/>
</p>
<p class="formBox">
<label for="email">Email</label>
<input name="email" id="email" type="email" title="built-in validation" required />
</p>
<p class="formBox">
<label for="phone">Phone</label>
<input name="phone" id="phone" type="tel" title="10 digit number 3 3 4 with spaces, hyphens or decimals between the sets" required pattern="([\d]{3})[-\. ]?[\d]{3}[-\. ]?[\d]{4}" />
</p>
<p>
<button id="btnSend">Send</button>
</p>
</form>
</main>
</body>
</html>
.formBox {
display: grid;
grid-template-columns: 20ch 200px;
margin-block: 1rem;
}
input,
label {
font-size: 1.2rem;
font-weight: 300;
padding: 0.2rem 1rem;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
button {
font-size: 1.2rem;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
font-weight: 300;
border: 1px solid steelblue;
padding: 0.2rem 2rem;
margin-inline-start: 20ch;
background-color: steelblue;
color: white;
}
document.getElementById('sampleForm').addEventListener('submit', (ev) => {
ev.preventDefault();
alert('success');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment