-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added field validation in the UI (#28)
Issue: #13
- Loading branch information
Showing
5 changed files
with
53 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
let form = document.getElementById("form"); | ||
let handler =()=> { | ||
console.log("validation triggered"); | ||
event.preventDefault(); // Prevent default form submission | ||
|
||
validateForm() | ||
} | ||
['submit', 'blur', 'focusout'].forEach(event => form.addEventListener(event, handler)); | ||
|
||
function validateForm() { | ||
let isValid = true; | ||
let errorMessage = '' | ||
|
||
const pem = document.getElementById('pem'); | ||
if (pem.value.trim() === '') { | ||
isValid = false; | ||
errorMessage = "Must submit a PEM file" | ||
} | ||
|
||
if (pem.value.includes("PRIVATE")) { | ||
isValid = false; | ||
errorMessage = "It seems a private key is entered, do not submit a private key! Even though we do not store anything you should never submit a private key!!" | ||
} | ||
|
||
if (!isLikelyPEM(pem.value.trim())) { | ||
isValid = false; | ||
errorMessage = "The file you provided does not seem to be a PEM file" | ||
} | ||
|
||
pem.setAttribute('aria-invalid', !isValid) | ||
const helperText = document.getElementById('pem-helper'); | ||
helperText.textContent = isValid ? "detected PEM file" : errorMessage; | ||
} | ||
|
||
function isLikelyPEM(input) { | ||
const pemRegex = /-----BEGIN (?!PRIVATE KEY)([A-Z0-9 ]+)-----[\s\S]+?-----END \1-----/g; | ||
return pemRegex.test(input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,12 +13,13 @@ | |
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.colors.min.css" | ||
/> | ||
<script src="https://unpkg.com/[email protected]"></script> | ||
<script defer type = "text/javascript" src="assets/js/validation.js"></script> | ||
<title>PEM Parser</title> | ||
<link rel="icon" type="image/x-icon" sizes="48x48" href="assets/favicon.ico"/> | ||
|
||
<style> | ||
:root { | ||
--pico-border-radius: 1rem; | ||
--pico-border-radius: 0.5rem; | ||
--pico-font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace; | ||
--pico-typography-spacing-vertical: 0.5rem; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters