final version

This commit is contained in:
ak 2023-06-21 22:44:25 -07:00
parent f81ca65798
commit 1191fc0f68
16 changed files with 16555 additions and 207 deletions

11
.babelrc Normal file
View file

@ -0,0 +1,11 @@
{
"plugins": ["@babel/syntax-dynamic-import"],
"presets": [
[
"@babel/preset-env",
{
"modules": false
}
]
]
}

10
.eslintrc.js Normal file
View file

@ -0,0 +1,10 @@
module.exports = {
"extends": [
"eslint-config-prettier",
"prettier"
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
}
}

12
.prettierignore Normal file
View file

@ -0,0 +1,12 @@
node_modules/
dist/
src/style.css
src/thumbsup.gif
.babelrc
.eslintrc.js
.prettierignore
.prettierrc.json
index.html
package-lock.json
package.json
README.md

1
.prettierrc.json Normal file
View file

@ -0,0 +1 @@
{}

View file

@ -1,2 +1,15 @@
# js-validationform # 🚀 Welcome to your new awesome project!
This project has been created using **webpack-cli**, you can now run
```
npm run build
```
or
```
yarn build
```
to bundle your application

146
dist/main.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -21,7 +21,7 @@
<main class="col-sm-6 py-3"></main> <main class="col-sm-6 py-3"></main>
</center> </center>
</div> </div>
<script type="module" src="/src/index.js"></script> <script type="module" src="/dist/main.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.9.2/umd/popper.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.9.2/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.0/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.0/js/bootstrap.min.js"></script>
</body> </body>

16134
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,19 +1,30 @@
{ {
"name": "js-validationform", "name": "my-webpack-project",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "My webpack project",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --mode=development",
"watch": "webpack --watch"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "ssh://_gitea@git.alexanderk.net:3022/ak/js-validationform.git" "url": "ssh://_gitea@git.alexanderk.net:3022/ak/js-validationform.git"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "ak",
"license": "ISC", "license": "AGPL-3.0-or-later",
"devDependencies": { "devDependencies": {
"@babel/core": "^7.22.5",
"@babel/preset-env": "^7.22.5",
"@webpack-cli/generators": "^3.0.7",
"babel-loader": "^9.1.2",
"css-loader": "^6.7.3",
"eslint": "^8.43.0",
"eslint-config-prettier": "^8.8.0",
"mini-css-extract-plugin": "^2.7.6",
"style-loader": "^3.3.1",
"webpack": "^5.88.0", "webpack": "^5.88.0",
"webpack-cli": "^5.1.4" "webpack-cli": "^5.1.4"
} }

View file

@ -1,10 +1,10 @@
// email field validation // email field validation
export function emailValidation() { export function emailValidation() {
// finds "@"" and "." in inputted email address // finds "@"" and "." in inputted email address
const findAt = new RegExp(/[@]/, 'gi'); const findAt = new RegExp(/[@]/, "gi");
const findDot = new RegExp(/[\.]/, 'gi'); const findDot = new RegExp(/[\.]/, "gi");
if (findAt.test(this.value) && findDot.test(this.value)) { if (findAt.test(this.value) && findDot.test(this.value)) {
this.setCustomValidity(''); this.setCustomValidity("");
this.reportValidity(); this.reportValidity();
return; return;
} }

View file

@ -1,5 +1,5 @@
import { makeWindow } from "./makeWindow.js"; import { makeWindow } from "./makeWindow.js";
const NEWBUTTON = document.getElementById('newButton'); const NEWBUTTON = document.getElementById("newButton");
NEWBUTTON.onclick = () => makeWindow(); NEWBUTTON.onclick = () => makeWindow();

View file

@ -1,11 +1,11 @@
// input validation for input fields without specific requirements // input validation for input fields without specific requirements
export function inputValidation(originalValue) { export function inputValidation(originalValue) {
// if the user has not entered a value different from the original, highlight and pop up message // if the user has not entered a value different from the original, highlight and pop up message
if (this.value === originalValue || this.value === '') { if (this.value === originalValue || this.value === "") {
this.setCustomValidity("Please fill out this field"); this.setCustomValidity("Please fill out this field");
this.reportValidity(); this.reportValidity();
return; return;
} }
this.setCustomValidity(''); this.setCustomValidity("");
this.reportValidity(); this.reportValidity();
} }

View file

@ -1,22 +1,22 @@
import { zipValidation } from "./zipValidation.js" import { zipValidation } from "./zipValidation.js";
import { emailValidation } from "./emailValidation.js"; import { emailValidation } from "./emailValidation.js";
import { inputValidation } from "./inputValidation.js"; import { inputValidation } from "./inputValidation.js";
import { passwordValidation } from "./passwordValidation.js"; import { passwordValidation } from "./passwordValidation.js";
import { submissionValid } from "./submissionValidation.js"; import { submissionValid } from "./submissionValidation.js";
const BODY = document.querySelector('main'); const BODY = document.querySelector("main");
// close button functionality // close button functionality
const closeWindow = (headDiv) => { const closeWindow = (headDiv) => {
headDiv.parentElement.removeChild(headDiv); headDiv.parentElement.removeChild(headDiv);
} };
// handy way of making new elements // handy way of making new elements
const makeElement = (passedClass, passedType = 'div') => { const makeElement = (passedClass, passedType = "div") => {
const newElement = document.createElement(passedType); const newElement = document.createElement(passedType);
newElement.setAttribute('class', passedClass); newElement.setAttribute("class", passedClass);
return newElement; return newElement;
} };
const thumbsUp = () => { const thumbsUp = () => {
// remove input fields // remove input fields
@ -26,33 +26,42 @@ const thumbsUp = () => {
// remove submit button // remove submit button
formWindow.removeChild(formWindow.lastChild); formWindow.removeChild(formWindow.lastChild);
// add thumbs up // add thumbs up
const thumbsUpGIF = document.createElement('img'); const thumbsUpGIF = document.createElement("img");
thumbsUpGIF.src = "/src/thumbsup.gif"; thumbsUpGIF.src = "/src/thumbsup.gif";
formWindow.appendChild(thumbsUpGIF); formWindow.appendChild(thumbsUpGIF);
} };
export const makeWindow = () => { export const makeWindow = () => {
// *make window itself* // *make window itself*
// 1. window body // 1. window body
const formWindow = makeElement('card-body', 'form'); const formWindow = makeElement("card-body", "form");
formWindow.id = 'formWindow'; formWindow.id = "formWindow";
BODY.appendChild(makeElement('card')).appendChild(formWindow); BODY.appendChild(makeElement("card")).appendChild(formWindow);
// 2. close button div // 2. close button div
const closeButton = makeElement('mb-3 d-flex'); const closeButton = makeElement("mb-3 d-flex");
formWindow.appendChild(closeButton); formWindow.appendChild(closeButton);
// the close button is SVG // the close button is SVG
// 3. container for SVG - 24x24 // 3. container for SVG - 24x24
const closeButtonSVGContainer = document.createElementNS("http://www.w3.org/2000/svg","svg"); const closeButtonSVGContainer = document.createElementNS(
"http://www.w3.org/2000/svg",
"svg"
);
closeButtonSVGContainer.style = "width: 24px; height: 24px"; closeButtonSVGContainer.style = "width: 24px; height: 24px";
closeButtonSVGContainer.setAttribute('viewbox', '0 0 24 24'); closeButtonSVGContainer.setAttribute("viewbox", "0 0 24 24");
closeButtonSVGContainer.id = 'closeButton'; closeButtonSVGContainer.id = "closeButton";
closeButton.appendChild(closeButtonSVGContainer); closeButton.appendChild(closeButtonSVGContainer);
// 4. the actual SVG path // 4. the actual SVG path
const closeButtonSVGPath = document.createElementNS("http://www.w3.org/2000/svg","path"); const closeButtonSVGPath = document.createElementNS(
closeButtonSVGPath.setAttribute('d', "M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z"); "http://www.w3.org/2000/svg",
"path"
);
closeButtonSVGPath.setAttribute(
"d",
"M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z"
);
closeButtonSVGPath.fill = "#6c757d"; closeButtonSVGPath.fill = "#6c757d";
closeButtonSVGContainer.appendChild(closeButtonSVGPath); closeButtonSVGContainer.appendChild(closeButtonSVGPath);
// add event listeners to close the window if button is clicked // add event listeners to close the window if button is clicked
@ -61,7 +70,7 @@ export const makeWindow = () => {
// *make input fields* // *make input fields*
// generate container divs // generate container divs
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
const inputContainer = makeElement('mb-3'); const inputContainer = makeElement("mb-3");
inputContainer.id = `input-${i + 1}`; inputContainer.id = `input-${i + 1}`;
formWindow.appendChild(inputContainer); formWindow.appendChild(inputContainer);
} }
@ -69,62 +78,62 @@ export const makeWindow = () => {
const originalValues = []; const originalValues = [];
// 5. email field // 5. email field
const userEmail = makeElement('form-control', 'input'); const userEmail = makeElement("form-control", "input");
userEmail.type = 'text'; userEmail.type = "text";
userEmail.value = 'Email address'; userEmail.value = "Email address";
originalValues.push(userEmail.value); originalValues.push(userEmail.value);
document.getElementById('input-1').appendChild(userEmail); document.getElementById("input-1").appendChild(userEmail);
// 6. country field // 6. country field
const userCountry = makeElement('form-control', 'input'); const userCountry = makeElement("form-control", "input");
userCountry.type = 'text'; userCountry.type = "text";
userCountry.value = 'Country'; userCountry.value = "Country";
originalValues.push(userCountry.value); originalValues.push(userCountry.value);
document.getElementById('input-2').appendChild(userCountry); document.getElementById("input-2").appendChild(userCountry);
// 7. zip code field // 7. zip code field
const userZip = makeElement('form-control', 'input'); const userZip = makeElement("form-control", "input");
userZip.type = 'text'; userZip.type = "text";
userZip.value = 'Zip code'; userZip.value = "Zip code";
originalValues.push(userZip.value); originalValues.push(userZip.value);
document.getElementById('input-3').appendChild(userZip); document.getElementById("input-3").appendChild(userZip);
// only allow numeric input // only allow numeric input
userZip.oninput = (e) => { userZip.oninput = (e) => {
let numericValue = e.target.value.replace(new RegExp(/[^\d]/,'gi'), ""); let numericValue = e.target.value.replace(new RegExp(/[^\d]/, "gi"), "");
numericValue = numericValue.substring(0, 5); numericValue = numericValue.substring(0, 5);
e.target.value = numericValue; e.target.value = numericValue;
} };
// 8. password field // 8. password field
const userPass = makeElement('form-control', 'input'); const userPass = makeElement("form-control", "input");
userPass.type = 'text'; userPass.type = "text";
userPass.value = 'Password'; userPass.value = "Password";
originalValues.push(userPass.value); originalValues.push(userPass.value);
document.getElementById('input-4').appendChild(userPass); document.getElementById("input-4").appendChild(userPass);
// 9. password confirmation field // 9. password confirmation field
const userPassConfirm = makeElement('form-control', 'input'); const userPassConfirm = makeElement("form-control", "input");
userPassConfirm.type = 'text'; userPassConfirm.type = "text";
userPassConfirm.value = 'Confirm password'; userPassConfirm.value = "Confirm password";
originalValues.push(userPassConfirm.value); originalValues.push(userPassConfirm.value);
document.getElementById('input-5').appendChild(userPassConfirm); document.getElementById("input-5").appendChild(userPassConfirm);
// blank out the input fields when focused and change back when not focused // blank out the input fields when focused and change back when not focused
document.querySelectorAll('.form-control').forEach(inputField => { document.querySelectorAll(".form-control").forEach((inputField) => {
// keep original value for validation purposes // keep original value for validation purposes
const originalValue = inputField.value; const originalValue = inputField.value;
let oldValue = ''; let oldValue = "";
inputField.onfocus = () => { inputField.onfocus = () => {
// erases characters from input field when focused // erases characters from input field when focused
oldValue = inputField.value; oldValue = inputField.value;
inputField.value = ''; inputField.value = "";
// change password fields to hide password characters // change password fields to hide password characters
if (inputField === userPass || inputField === userPassConfirm) { if (inputField === userPass || inputField === userPassConfirm) {
inputField.type = 'password'; inputField.type = "password";
}
} }
};
inputField.onblur = (e) => { inputField.onblur = (e) => {
// change back input field value to the old value if unmodified // change back input field value to the old value if unmodified
if (inputField.value === '') { if (inputField.value === "") {
inputField.value = oldValue; inputField.value = oldValue;
} }
// email validation // email validation
@ -143,18 +152,23 @@ export const makeWindow = () => {
if (inputField === userPassConfirm) { if (inputField === userPassConfirm) {
passwordValidation.call(e.target, userPass.value); passwordValidation.call(e.target, userPass.value);
} }
} };
}); });
// 10. submit button // 10. submit button
const submitButton = makeElement('btn btn-primary', 'button'); const submitButton = makeElement("btn btn-primary", "button");
submitButton.type = 'submit'; submitButton.type = "submit";
submitButton.textContent = "Submit"; submitButton.textContent = "Submit";
formWindow.appendChild(submitButton); formWindow.appendChild(submitButton);
// prevent default // prevent default
submitButton.onclick = (event) => { submitButton.onclick = (event) => {
event.preventDefault(); event.preventDefault();
if (submissionValid(document.querySelectorAll('.form-control'), originalValues)) { if (
submissionValid(
document.querySelectorAll(".form-control"),
originalValues
)
) {
// give em a thumbs up // give em a thumbs up
thumbsUp(); thumbsUp();
return; return;
@ -162,4 +176,4 @@ export const makeWindow = () => {
// or tell them to fix it // or tell them to fix it
alert("Please properly fill out all fields"); alert("Please properly fill out all fields");
}; };
} };

View file

@ -2,7 +2,7 @@
export function passwordValidation(origPassword) { export function passwordValidation(origPassword) {
// check if passwords match // check if passwords match
if (this.value === origPassword) { if (this.value === origPassword) {
this.setCustomValidity(''); this.setCustomValidity("");
this.reportValidity(); this.reportValidity();
return; return;
} }

View file

@ -3,15 +3,17 @@ export const submissionValid = (inputFields, originalValues) => {
// checks each field for validity // checks each field for validity
let bool = true; let bool = true;
let arrNum = 0; let arrNum = 0;
inputFields.forEach(inputField => { inputFields.forEach((inputField) => {
// if the field is previously invalid, unchanged or blank declare validity to be false // if the field is previously invalid, unchanged or blank declare validity to be false
if (!(inputField.validity.valid) || if (
!inputField.validity.valid ||
inputField.value === originalValues[arrNum] || inputField.value === originalValues[arrNum] ||
inputField === '') { inputField === ""
) {
bool = false; bool = false;
} }
arrNum++; arrNum++;
}); });
// returns final validity // returns final validity
return bool; return bool;
} };

View file

@ -1,5 +1,5 @@
// zip field validation // zip field validation
const LENGTH = 5 const LENGTH = 5;
export function zipValidation() { export function zipValidation() {
// if zip code is under 5 digits, highlight field and pop up message // if zip code is under 5 digits, highlight field and pop up message
if (this.value.length < LENGTH) { if (this.value.length < LENGTH) {
@ -7,6 +7,6 @@ export function zipValidation () {
this.reportValidity(); this.reportValidity();
return; return;
} }
this.setCustomValidity(''); this.setCustomValidity("");
this.reportValidity(); this.reportValidity();
} }